0

I want to create a jTree which when i right click on a node, should give me the options of "rename","add Region(parent)","add City(child)".

the name of my jTree is branches

As i am new to swing,Could any one help with code. Thanks in Advance.

Regards, Sarkwa

jzd
  • 23,473
  • 9
  • 54
  • 76
Sarkwa
  • 1
  • 1
  • 5

2 Answers2

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

Community
  • 1
  • 1
alex2410
  • 10,904
  • 3
  • 25
  • 41
2

Steps:

  • Add a MouseListner to the JTree
  • Have the mouse listener only respond to events from Button3 (right click).
  • Have the listner's action display a JPopupMenu.
  • In the menu add your options
  • Your options will have actions that will need to have a reference back to the JTree for the appropriate modifications to happen.
jzd
  • 23,473
  • 9
  • 54
  • 76
  • can u pls give me an example of how this is done? will appreciate that – Sarkwa Nov 04 '13 at 16:02
  • 1
    Which step? It sounds like you need to read some of the Swing Tutorials they have plenty of examples. – jzd Nov 04 '13 at 16:09