Solved

get data from JTree after clicking on a node

Posted on 1998-11-02
8
229 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
  • 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now