Zolf
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)tr
selectedNode.getLevel()
but this does not work when i right click on the nodes.i get this error java.lang.NullPointerExcep
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)tr
treePopupMenu.show(me.getC
System.out.println(selecte
}
}
});
private JPopupMenu createPopupMenuDslam()
{
final JPopupMenu pm = new JPopupMenu();
JMenuItem addItem = new JMenuItem(" Add ");
addItem.setActionCommand("
addItem.addActionListener(
public void actionPerformed(ActionEven
JOptionPane.showConfirmDia
}
});
pm.add(addItem);
JMenuItem editItem = new JMenuItem(" Edit ");
editItem.setActionCommand(
editItem.addActionListener
public void actionPerformed(ActionEven
JOptionPane.showConfirmDia
}
});
pm.add(editItem);
pm.add(new JSeparator());
JMenuItem deleteItem = new JMenuItem(" Delete ");
deleteItem.setActionComman
deleteItem.addActionListen
public void actionPerformed(ActionEven
JOptionPane.showConfirmDia
}
});
pm.add(deleteItem);
return pm;
}
cheers
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
> 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
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.
ASKER
ok now i dont get the error when i remove that getLastSelectedPathCompone
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());
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you need to eaith:
first select the node
or don't use getLastSelectedPathCompone
you can get the path using:
TreePath selPath = tree.getPathForLocation(me