problem_using_JTree
Hi boys,
If I use JTree, what can I use to realize a menu with command, for example, like "cut", "open", "delete", that I can open clicking with the mouse's right button on an element of tree?
If it isn't clear, tell me
please, help me!!!
thanks in advance.....
lucignolo
I have a JTree, with one root and some child; now, when I click with the right button on a child, I will a menu that give me the possibility of making operations like delete child, view child,etc.....the some thing that you click one time with the right button on an icon of your desktop.
How can I do to create this menu using Java, and call it clicking the right button of the mouse on the selected item of JTree?
now, is it clear? :-)
Heres how to bring up a popup menu:
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#popup
Register a mouse listener to the tree, on right click, bring up the menu. The tricky part is dealing with context sensitve actions - you can query the selected node for actions - and having those actions obtain the node context to perform their work on. Good luck.
for IanSchneider
I'm not understand how to associate the opening of popup menu when I click on an element of the tree with the right button.
If I register a mouse listener to the JTree, clicking with the right button in every point of JTree will be open a popup menu; I need to open a popup menu when I click on an element of the Tree, not when I click on JTree.
Can you help me?
thanks in advance!!
lucignolo
Check out getPathForLocation(int x, int y). It returns a TreePath whose last
element is the thing you want.
It is documented here: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTree.html
If I recall correctly, Sun's tutorial has a good example dynamically changing
a tree's contents.