Puoi disabilitare l'intero componente grafico mxGraphComponent
:
graphComponent.setEnabled(false);
Domanda
Non sono riuscito a trovare una risposta nel manuale o su SO.Voglio creare un grafico con JGraphX che mostri alcune verità e bordi, ma non voglio che l'utente sia in grado di spostare nulla, né che quelle caselle di modifica verdi appaiano sui vortici o sui bordi.È solo per la visualizzazione.
Ho provato questa modifica dell'esempio "Hello World" senza alcun risultato.Eventuali suggerimenti?
package com.mxgraph.examples.swing;
import java.util.Hashtable;
import javax.swing.JFrame;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;
public class HelloWorld extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;
public HelloWorld()
{
super("Hello, World!");
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
//my addition of a stylesheet, I used it on the first node to see if it mad
//a difference, it didn't regarding dragability//////////////////////////////
mxStylesheet stylesheet = graph.getStylesheet();
Hashtable<String, Object> style = new Hashtable<String, Object>();
style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_ELLIPSE);
style.put(mxConstants.STYLE_OPACITY, 50);
style.put(mxConstants.STYLE_FONTCOLOR, "#774400");
style.put(mxConstants.STYLE_EDITABLE, false);
stylesheet.putCellStyle("ROUNDED", style);
//tried this too///////////////////////////////////////////////
graph.setCellsEditable(false);
graph.getModel().beginUpdate();
try
{
Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
30, "ROUNDED");
Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
80, 30);
graph.insertEdge(parent, null, "Edge", v1, v2);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
HelloWorld frame = new HelloWorld();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
Soluzione
Puoi disabilitare l'intero componente grafico mxGraphComponent
:
graphComponent.setEnabled(false);
Altri suggerimenti
Potresti sovrascrivere isCellSelectable
per impedire la selezione delle celle
mxGraph graph = new mxGraph() {
@Override
public boolean isCellSelectable(Object cell) {
if (cell != null) {
if (cell instanceof mxCell) {
mxCell myCell = (mxCell) cell;
if (myCell.isEdge())
return false;
}
}
return super.isCellSelectable(cell);
}
};