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

[311 byte] By [lucignoloa] at [2007-10-3 4:53:38]
# 1
> If it isn't clear, tell meit isn't clear
SoulTech2012a at 2007-7-14 22:58:30 > top of Java-index,Java Essentials,Java Programming...
# 2

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? :-)

lucignoloa at 2007-7-14 22:58:31 > top of Java-index,Java Essentials,Java Programming...
# 3

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.

IanSchneidera at 2007-7-14 22:58:31 > top of Java-index,Java Essentials,Java Programming...
# 4

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

lucignoloa at 2007-7-14 22:58:31 > top of Java-index,Java Essentials,Java Programming...
# 5

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.

pbrockway2a at 2007-7-14 22:58:31 > top of Java-index,Java Essentials,Java Programming...
# 6
the problem is solved, thanks boys for helping me!!!lucignolo
lucignoloa at 2007-7-14 22:58:31 > top of Java-index,Java Essentials,Java Programming...