Sarkwa
Sarkwa

Reputation: 1

creating a right- click menu pop-up on a jTree

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

Upvotes: 0

Views: 6372

Answers (2)

alex2410
alex2410

Reputation: 10994

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

Upvotes: 4

jzd
jzd

Reputation: 23629

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.

Upvotes: 2

Related Questions