Solved

JTree MouseListener problem

Posted on 2007-03-28
8
1,033 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
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!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

710 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