wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Adding the proper listener to a JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding the proper listener to a JTree" Watch "Adding the proper listener to a JTree" New topic
Author

Adding the proper listener to a JTree

Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

There are a couple of listeners with a JTree. Which one should I use if I want, when a leaf node is clicked, so for it to open a new window or something?

DefaultMutableTreeNode itself doesn't seem to have any listeners associated with it. I'm just not quite sure how to go about this.



Repeal Obamacare.
http://www.dontfundobamacare.com/
http://liberty-amendments.freeforums.net/
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
TreeSelectionListener
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

Michael Dunn wrote:TreeSelectionListener


How do I know which TreeNode it was? Can I use getSource(), or, like I'm thinking, will it return the JTree itself as the source no matter Node is being selected?

Say I have a DefaultMutableTreeNode?

DefaultMutableTreeNode dmtn, dmtn2;

dmtn = new DefaultMutableTreeNode("Top element 1", true);

dmtn2 = new DefaultMutableTreeNode("Sub element 1A", false);

dmtn.add(dmtn2);

Now, can I try, assuming I have a TreeSelectionEvent e

if (e.getSource() == dmtn2)
{
// do something

}

I'm not sure how to work the TreePath thing with a TreeNode.

I see that I might be able to get it to work with getPathComponent(int index) but will the getSource() work as well? Just wondering.

What will getPathComponent() be returning, the data of the Tree, a String, or the TreeNode component? How do I know if this TreeNode is selected? I don't want it triggering the thing automatically without being selected.

Or do I use JTree's getLastSelectionPathComponent() or whatever it was called?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

If you were to read the API for TreeSelectionListener, you would find a link to the tutorial on How to Write a Tree Selection Listener. At the top of that page is an example that details the course you need to follow.


luck, db
There are no new questions, but there may be new answers.
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

Darryl Burke wrote:If you were to read the API for TreeSelectionListener, you would find a link to the tutorial on How to Write a Tree Selection Listener. At the top of that page is an example that details the course you need to follow.


Suppose I wanted to have more than one path visible at a time though?


Root Node
subnode
sub sub node
subnode 2
sub sub 2 node
subnode 3
sub sub 3 node

And I wanted sub sub 2 node, when selected, to do something, but I wanted it possible to display all the tree paths and not block it so that only one can show at a time.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

I'm lost. I don't really understand what you're asking. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem, and don't forget to UseCodeTags to post your code.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Paul Adcock wrote:but I wanted it possible to display all the tree paths and not block it so that only one can show at a time.


But this is normal behaviour for JTree.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding the proper listener to a JTree
 
Similar Threads
JTree + JTabel
JTree and Filesystem?
collapsing JTree
JTree not updating
JTree