Solved

get data from JTree after clicking on a node

Posted on 1998-11-02
8
267 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 100 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
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!

 

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

Technology Partners: 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

726 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