Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

show different popupmenu


hello there,

i have a JTree which gets populated from the db.the structure of my Jtree is like this

-c
 -d
   -f
    -g

now i want to have a popupmenu for each of those nodes.i mean i want to have different popup menu for each nodes.for example the parent node c will have "add c","delete c","edit c"    node d will have "add d","delete d","edit d" and so on.
i tried to use

DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
selectedNode.getLevel()
 but this does not work when i right click on the nodes.i get this error java.lang.NullPointerException.but if i left click it first then right clcik it sjows me the level.

also i want to know how do i implement my code so the i get different pop menu according to the nodes i right clcik.
my code below shows the same popup menu for anywhere i click on my jtree

//constr
treePopupMenu = createPopupMenuDslam();

public void mouseReleased(MouseEvent me)
              {
                    if (me.isPopupTrigger())
                    {
                          DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                          treePopupMenu.show(me.getComponent(), me.getX(), me.getY());
                          System.out.println(selectedNode.getLevel());
                    }
              }
        });

private JPopupMenu createPopupMenuDslam()
    {
          final JPopupMenu pm = new JPopupMenu();
          
          JMenuItem addItem = new JMenuItem("  Add  ");
          addItem.setActionCommand("add");
          addItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                      JOptionPane.showConfirmDialog(pm, "Was pressed popup menu ADD item");
                }
          });
          pm.add(addItem);
          
          JMenuItem editItem = new JMenuItem("  Edit  ");
          editItem.setActionCommand("edit");
          editItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                      JOptionPane.showConfirmDialog(pm, "Was pressed popup menu EDIT item");
                }
          });
          pm.add(editItem);
          
          pm.add(new JSeparator());
          
          JMenuItem deleteItem = new JMenuItem("  Delete  ");
          deleteItem.setActionCommand("delete");
            deleteItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                      JOptionPane.showConfirmDialog(pm, "Was pressed popup menu DELETE item");
                }
          });         
          pm.add(deleteItem);
          
          return pm;
    }

cheers
zolf
Avatar of Mick Barry
Mick Barry
Flag of Australia image

>  but this does not work when i right click on the nodes.i get this error java.lang.NullPointerException.but if i left click it first then right clcik it sjows me the level.

you need to eaith:

first select the node
or don't use getLastSelectedPathComponent()

you can get the path using:

TreePath selPath = tree.getPathForLocation(me.getX(), me.getY());
Avatar of Zolf

ASKER



you need to eaith: ???
eaith == either :)

> also i want to know how do i implement my code so the i get different pop menu according to the nodes i right clcik.

perhaps keep a Map of popups and select which popup to display based on some attribute of the selected node
Avatar of Zolf

ASKER


but i cannot right clcik on the nodes the first time.it give me error.
but if i left click and then right click it works fine.how can i directly right clcik on the node to get its level.
Avatar of Zolf

ASKER


ok now i dont get the error when i remove that getLastSelectedPathComponent().
but i need to know the level of the tree to know whick popup menu to show.is there some how i can get the level.
> but i cannot right clcik on the nodes the first time.it give me error.

no, your code is giving you an error because there is no selected node.

> how can i directly right clcik on the node to get its level.

TreePath selPath = tree.getPathForLocation(me.getX(), me.getY());
Avatar of Zolf

ASKER


ic,thanks mate.

so the first bit is solved.now regarding the other part that i need to show different popup menu according to the nodes clciked.
presently i am implementing the popupmenu as i have shown above.how do i change this now.can you help
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial