Pergunta

Não consegui encontrar uma resposta no manual, ou assim. Quero fazer um gráfico com o JGRAPHX que exibe alguns verices e bordas, mas não quero que o usuário possa mover qualquer coisa por volta, nem para que essas caixas de edição verde apareçam nos vórtices ou bordas. É apenas para exibição apenas.

Eu tentei esta modificação do exemplo "Hello World" sem sucesso. Alguma sugestão?

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);
}

}
Foi útil?

Solução

Você pode desativar o componente do gráfico inteiro mxGraphComponent:

graphComponent.setEnabled(false);

Outras dicas

Você pode substituir isCellSelectable Para evitar a seleção de células

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);
      }
};
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top