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
Java

Avatar of undefined
Last Comment
Mick Barry
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
Zolf
Flag of United Arab Emirates image

ASKER



you need to eaith: ???
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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
Zolf
Flag of United Arab Emirates image

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
Zolf
Flag of United Arab Emirates image

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.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> 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
Zolf
Flag of United Arab Emirates image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo