JTG在JGraphX中

时间:2013-02-26 01:36:57

标签: java jgraphx

在EditorPalette中,模板使用JLabel表示,可以在mxGraphComponent中拖放,对吗?

但是,我想通过JTree使用层次结构将这些模板添加到EditorPalette,并且不能像普通模板那样在GraphComponents中拖放节点

您能否通过提供添加模板的功能来帮助我,以便在组件的左侧添加JTree并在mxGraphComponent上拖放?

1 个答案:

答案 0 :(得分:1)

好的,我明白了。我的错误是,我必须创建一个提供几何信息的mxCell,而不是

mxCell cell = new mxCell(component);

在上面的代码我必须写

mxCell cell = new mxCell(component, new mxGeometry(), "");

添加拖动功能的完整方法如下所示:

public void addComponentTabListeners() {
    final JTree componentTree = view.componentTree;

    DragGestureListener dragGestureListener = new DragGestureListener() {
        @Override
        public void dragGestureRecognized(DragGestureEvent arg0) {
            TreePath path = componentTree.getSelectionPath();
            if ((path == null) || (path.getPathCount() <= 1)) {
                return;
            }
            DefaultMutableTreeNode componentsNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            I_Component component = null;
            try {
                component = ComponentHandler_KonsensApplication.getInstance().createInstance(
                        componentsNode.toString(), "need unique Name here");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            mxCell cell = new mxCell(component, new mxGeometry(), "");

            cell.setVertex(true);
            mxRectangle bounds = new mxRectangle();
            bounds.setHeight(80);
            bounds.setWidth(80);
            mxGraphTransferable t = new mxGraphTransferable(new Object[] { cell }, bounds);
            arg0.startDrag(null, mxSwingConstants.EMPTY_IMAGE, new Point(), t, null);
        }
    };

    DragSource dragSource = new DragSource();
    dragSource.createDefaultDragGestureRecognizer(componentTree, DnDConstants.ACTION_COPY,
            dragGestureListener);
}