tags:

views:

144

answers:

1

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?

+2  A: 

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.

Aaron Digulla
dammmnn I knew it had to do with the viewport. Thanks!
Hezeus