JGraphX - 希望整个图形都不可编辑

5

我在手册或SO上没有找到答案。我想使用JGraphX制作一个显示一些顶点和边的图形,但是我不希望用户能够移动任何内容,也不希望那些绿色的编辑框出现在顶点或边上。这只是用于显示而已。

我尝试了“Hello World”示例的修改,但没有成功。有什么建议吗?

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

}
2个回答

11

您可以禁用整个图形组件 mxGraphComponent

graphComponent.setEnabled(false);

3
你可以重写isCellSelectable来防止单元格被选中。
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);
      }
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接