How can I get the coordinates of a node (JLabel,image, ..) on a JTree?

Sandip Chitale

Use any one of the following methods to get rectangular bounds of a cell -

public Rectangle getRowBounds(int row)
Returns the Rectangle that the node at the specified row is drawn in.
row - the row to be drawn, where 0 is the first row in the display
the Rectangle the node is drawn in

public Rectangle getPathBounds(TreePath path)
Returns the Rectangle that the specified node will be drawn into. Returns null if any component in the path is hidden (under a collapsed parent). 
This method returns a valid rectangle, even if the specified node is not currently displayed.

path - the TreePath identifying the node
the Rectangle the node is drawn in, or null

rectangle.x and rectangle.y should give you the coordinates relative to the tree.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact