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
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.
SCJP 1.4, www.gsi3d.org.uk
posted 18 years ago
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