• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding the proper listener to a JTree

 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TreeSelectionListener
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic