?
Solved

get data from JTree after clicking on a node

Posted on 1998-11-02
8
Medium Priority
?
275 Views
Last Modified: 2010-03-30
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
Comment
Question by:rzvika3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Expert Comment

by:martinag
ID: 1226879
tree.addTreeSelectionListener(new TreeSelectionListener() {
  public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.getPath().getLastPathComponent();
    Object o = node.getUserObject();
    ...
  }
});

Martin
0
 
LVL 4

Expert Comment

by:martinag
ID: 1226880
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
 
LVL 8

Accepted Solution

by:
diakov earned 300 total points
ID: 1226881
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rzvika3
ID: 1226882
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
 
LVL 8

Expert Comment

by:diakov
ID: 1226883
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
 

Author Comment

by:rzvika3
ID: 1226884
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
 
LVL 8

Expert Comment

by:diakov
ID: 1226885
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
 

Author Comment

by:rzvika3
ID: 1226886
Adjusted points to 100
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question