How to detect which JTree was clicked?

R

Ramon F Herrera

My application has two JTrees side by side, and I need to keep track
of the currently selected node, as follows:

public void valueChanged(TreeSelectionEvent e) {

pickedNode = (DefaultMutableTreeNode) leftTree.getLastSelectedPathComponent();
[...]
}


As you can see above, the 'e' variable is not being used, but it should.
So, how do I narrow down the TreeSelectionEvent, 'e' to the specific
JTree where the event was triggered??

TIA,

-Ramon F. Herrera
 
J

Jacob

Ramon said:
My application has two JTrees side by side, and I need to keep track
of the currently selected node, as follows:

public void valueChanged(TreeSelectionEvent e) {

pickedNode = (DefaultMutableTreeNode) leftTree.getLastSelectedPathComponent();
[...]
}


As you can see above, the 'e' variable is not being used, but it should.
So, how do I narrow down the TreeSelectionEvent, 'e' to the specific
JTree where the event was triggered??

e.getSource() will return you the source (i.e. JTree) of the event.
 
M

Martijn van Steenbergen

You could use two different TreeSelectionListeners to solve this
problem. Or, if I'm not mistaken, you could also do:

JTree source = (JTree)e.getSource();

Martijn.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top