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
zolfAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
if using a map you'd create as manay popups as you need and store them in your map

popups.put("a", popup1);
popups.put("b", popup1);
popups.put("c", popup1);

where the string is some attribute available on your node

then you can lookup the popup accoring to the attribute of the node

JPopupMenu popup = (JPopupMenu) popups.get(selectedObject.getPopupType());
0
 
objectsCommented:
>  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());
0
 
zolfAuthor Commented:


you need to eaith: ???
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
objectsCommented:
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
0
 
zolfAuthor Commented:

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.
0
 
zolfAuthor Commented:

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.
0
 
objectsCommented:
> 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());
0
 
zolfAuthor Commented:

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
0
All Courses

From novice to tech pro — start learning today.