Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JTree - How to check if node is displayed?

Tags:

java

swing

jtree

Looking for how to traverse a JTree (can do that) and check to see each node to see if it's displayed (to the user) or not visible. Can't believe JTree doesn't have this function, maybe I'm missing something?

like image 781
Hezeus Avatar asked Feb 27 '23 01:02

Hezeus


1 Answers

You must consider two different things:

  1. A node can become hidden by closing one of its parents. Even though the parent is visible on the screen, the child isn't. Use JTree.isVisible() for this.

  2. If the node is expanded, it can become hidden because it is scrolled out of the current viewport. This isn't handled in the JTree but in the JScrollPane which wraps the tree. To find out if a node is in the visible area of the viewport.

To find out if #2 is true, you must get the rectangle where the node is using JTree.getPathBounds(). Then, you must intersect this rectangle with the viewport (use scrollPane.getViewport().getViewRect(). If nodeRect.intersects (viewRect) returns true, the node is visible.

like image 111
Aaron Digulla Avatar answered Mar 07 '23 10:03

Aaron Digulla