Solved

JTree MouseListener problem

Posted on 2007-03-28
8
1,025 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 90
eclipse java  build path 6 45
windows explorer path to command prompt 5 33
eclipse apache tomcat admin console 52 66
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…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

896 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