Bookmark Topic Watch Topic
  • New Topic

x-y position of JTree Selection

 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi,
I have a JTree, and I pop-up different menus according to the tree's selection. What I would like to do is to take into account where on the Tree's background the user just right-
clicked.
So, I can see lots of methods for returning a TreePath, or a row given an x-y- position pair, but what I want is to return an x-y position pair, given a row or Path.
e.g.
I have : getTree().getPathForLocation(x,y);
I want : get .getLocationForPath(TreePath);
Cheers,
Kate
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi,
You could just get the position of the mouse click within the component by using a MouseListener something along these lines

hope this helps
cheers, Ben.
 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi Ben,
yes, I can pick up the position of the cursor whenever it is cicked, but then what happens is I have to discard it if it did not represent a change in selected item (e.g. they just clicked to expand or collapse a branch).... alas, the tree changed event doesn't give me x-y position, and the mouse events can't tell me what sort of tree change the click represented.
So, I end up storing two sets of x-y pairs (tempX for all clicks, realX for when the last change really was a selection change) and a boolean doWeHaveAnUnrecordedTreeChangeEvent to get the position for ... all of which is clumsy and works fine, until I realised that of course, the user can alter the x-y position of the selected item for all sorts of reasons like they expanded the tree further up, they resized the window etc etc ...
I decided that keeping track of the current last selection via click was tricky, and counter to the java spirit : I should read the value as I need it - like with all the other controls.
There must be an easy method .... surely ???
Kate
 
Zach Hannand
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
Try:



Zach
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Zach,
Welcome to the Ranch.

If you have noticed, the original thread is almost 8 years old! I doubt if the original poster is waiting this long for the answer
 
Zach Hannand
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
I know, I posted that for the benefit of anyone who comes across this thread.

Zach
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!