Solved

JTree MouseListener problem

Posted on 2007-03-28
8
1,038 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
[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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

635 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