• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

get data from JTree after clicking on a node

as you all know, i have a JTree.
i want that each time i click on a node in the tree, i could get data from it. what method should i use and how can i get a reference to the tree node: Object ?
thank you.
0
rzvika3
Asked:
rzvika3
  • 3
  • 3
  • 2
1 Solution
 
martinagCommented:
tree.addTreeSelectionListener(new TreeSelectionListener() {
  public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.getPath().getLastPathComponent();
    Object o = node.getUserObject();
    ...
  }
});

Martin
0
 
martinagCommented:
This doesn't listen for clicks, actually it listens for selections. That happens when you click on another node than the one having focus or select using the arrow keys.

Martin
0
 
diakovCommented:
You can listen for clicks too, and then resolve on which one the click has occured.

 final JTree tree = ...;

 MouseListener ml = new MouseAdapter() {
     public void mouseClicked(MouseEvent e) {
         int selRow = tree.getRowForLocation(e.getX(), e.getY());
         TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
         if(selRow != -1) {
             if(e.getClickCount() == 1) {
                 mySingleClick(selRow, selPath);
             }
             else if(e.getClickCount() == 2) {
                 myDoubleClick(selRow, selPath);
             }
         }
     }
 };
 tree.addMouseListener(ml);

This does what you want. You receive the full qualifier of the clicked item and then you can retrieve the information.

Cheers,
  Nik


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rzvika3Author Commented:
ok, thank you both.
diakov, i see that in the code i can seperate between one click and two cliks. is there a way to know if it is right click (my final purpose is to popup a menue when right click on a node and in the menu wil be lets say "delete" and Add". if you can write the code to how adding rightclick listener(or something like that) and how i can know what the user pressed on (delete or add) please write. i will erase to 100 points.
thank you again!

0
 
diakovCommented:
I believe

((e.getModifiers() & e.BUTTON3_MASK) > 0)

will return true if the right button has been pressed.

In similar way you can check for CTRL button down while mouse clicking and etc.

Cheers,
  Nik
0
 
rzvika3Author Commented:
diakov, you didn't mentioned this part:
(my final purpose is to popup a menue when right click on a node and in the menu wil be lets say "delete" and Add". if you can write the code to how adding rightclick listener(or something like that) and how i can know what the user pressed on (delete or add) please write.

0
 
diakovCommented:
This is what I did.
I defined a

//I believe you can use some other pannel
rmenu = new symantec.itools.awt.BorderPanel();
rmenu.setBounds(0,0,68,40);
rmenu.setBackground(new Color(12632256));
//and then added several labels in it.
.
//this is the mouse listener for the menu
rmenu.addMouseListener(rmenu_listener);

//Then in the mouse listener of the tree:

if (modif == event.BUTTON3_MASK)
{
  Rectangle r = rmenu.getBounds();
  r.x = event.getX();
  r.y = event.getY();
  r.height = 68;
  rmenu.setBounds(r);
  rmenu.setVisible(true);
  rmenu.setEnabled(true);
}

in the listener for the menu you have to track how the mouse moves and change the color of the labels for example.

And there you go, you have a right click menu.

Cheers,
  Nik
0
 
rzvika3Author Commented:
Adjusted points to 100
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now