Solved

JTree MouseListener problem

Posted on 2007-03-28
8
1,024 Views
Last Modified: 2012-08-13
Hi,
I have a tree with nodes that contains a label and a checkbox.
I would like to behave differently if the checkbox is clicked or if the click is in different location in the node.
It all should be in context of the mouse listener implementation:
    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        ...
    }
is there a way of instructing JTree to display both checkbox and text but distinguish the pressing to one of them?
or is there an easier way?
Thanks.
0
Comment
Question by:aquasw
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:Naeemg
ID: 18806308
Add JPanel as ur tree node, and then add checkbox and JLabale to that each panel. then separately u can trace the click action.
0
 

Author Comment

by:aquasw
ID: 18806334
I thought of that.
I added my implementation of Jpanel and added a checkbox and label.
I tried to add a mouselistener at the constructing process to each but the pressing isn't getting to the listener.
0
 
LVL 92

Expert Comment

by:objects
ID: 18806409
you need to add your listener to the jtree and then determine from the coords of the click what was clicked.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 300 total points
ID: 18806598
Probably useful methods for you:
- int JTree.getRowForLocation(int x, int y)
  returns the row for the specified location.

- Rectangle JTree.getRowBounds(int row)
  returns the Rectangle that the node at the specified row is drawn in.

Example of a JTree with check boxes:
http://www.java2s.com/Code/Java/Swing-Components/CheckboxNodeTreeExample.htm

Success
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:aquasw
ID: 18806734
I know I can get the coordinates and check the bounds using a rectangle or any other....
I think it's a bit of avoiding the actual problem.
I don't want to manually calculate the area of the clicked item.
isn't there a way of adding a listener to the checkbox separately or the label?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 18806834
no you can't because the checkbox isn't actually a part of your tree, the renderer is just used to 'paint' the tree node. A checkbox is not added to the gui, thus you cannot add a listener to it.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 18806857
Exactly
0
 
LVL 37

Expert Comment

by:zzynx
ID: 18814069
thanx 4 axxepting
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now