JTree MouseListener problem

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?
Who is Participating?
zzynxConnect With a Mentor Software engineerCommented:
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:

Add JPanel as ur tree node, and then add checkbox and JLabale to that each panel. then separately u can trace the click action.
aquaswAuthor Commented:
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.
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

you need to add your listener to the jtree and then determine from the coords of the click what was clicked.
aquaswAuthor Commented:
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?
objectsConnect With a Mentor Commented:
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.
zzynxSoftware engineerCommented:
zzynxSoftware engineerCommented:
thanx 4 axxepting
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.