0

我想创建一个 jTree,当我右键单击一个节点时,它应该给我“重命名”、“添加区域(父)”、“添加城市(子)”的选项。

我的 jTree 的名称是分支

由于我是摇摆新手,任何人都可以帮助编写代码。提前致谢。

问候, Sarkwa

4

2 回答 2

4

I recommend you to use setComponentPopupMenu method of JTree with MouseListener. In mouseListener determine Node for menu and generate popupMenu once. I write a simple example which can help you to do your work.

public class Main extends javax.swing.JFrame {

private JTree t;
private DefaultTreeModel model;
private DefaultMutableTreeNode selectedNode;

public Main() {
    DefaultMutableTreeNode n = new DefaultMutableTreeNode("test");
    n.add(new DefaultMutableTreeNode("test2"));
    model = new DefaultTreeModel(n);
    t = new JTree(model);
    t.setEditable(true);
    t.setComponentPopupMenu(getPopUpMenu());
    t.addMouseListener(getMouseListener());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().add(t);
    pack();
    setVisible(true);
}

private MouseListener getMouseListener() {
    return new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent arg0) {
            if(arg0.getButton() == MouseEvent.BUTTON3){
                TreePath pathForLocation = t.getPathForLocation(arg0.getPoint().x, arg0.getPoint().y);
                if(pathForLocation != null){
                    selectedNode = (DefaultMutableTreeNode) pathForLocation.getLastPathComponent();
                } else{
                    selectedNode = null;
                }

            }
            super.mousePressed(arg0);
        }
    };
}

private JPopupMenu getPopUpMenu() {
    JPopupMenu menu = new JPopupMenu();
    JMenuItem item = new JMenuItem("edit");
    item.addActionListener(getEditActionListener());
    menu.add(item);

    JMenuItem item2 = new JMenuItem("add");
    item2.addActionListener(getAddActionListener());
    menu.add(item2);

    return menu;
}

private ActionListener getAddActionListener() {
    return new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(selectedNode != null){
                System.out.println("pressed" + selectedNode);
                DefaultMutableTreeNode n = new DefaultMutableTreeNode("added");
                selectedNode.add(n);
                t.repaint();
                t.updateUI();
            }
        }
    };
}

private ActionListener getEditActionListener() {
    return new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(selectedNode != null){
                //edit here
                System.out.println("pressed" + selectedNode);
            }
        }
    };
}

public static void main(String... s){
    new Main();
}

}

getPopUpMenu method generate your popUp. For all items in popUp I add Listener for actions. For renaming nodes I recommend you to use CellEditor instead of menu, i write simple example of using it here.

And read this tutorial for JTree

于 2013-11-07T14:58:43.803 回答
2

脚步:

  • 添加一个MouseListnerJTree
  • 让鼠标侦听器仅响应来自 Button3 的事件(右键单击)。
  • 让侦听器的操作显示JPopupMenu.
  • 在菜单中添加您的选项
  • 您的选项将具有需要引用回的操作以JTree进行适当的修改。
于 2013-11-04T15:35:46.267 回答