This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

x-y position of JTree Selection

 
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
 
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
Try:



Zach
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!