开发者

Automatically expand JTree to until height threshold is met

开发者 https://www.devze.com 2023-03-29 05:25 出处:网络
I have a JTree on a JScrollPane as part of my GUI. I\'ve set up an AncestorListener where I respond to the ancestorAdded event. In the event, I would like to automatically expand as manyvisible nodes

I have a JTree on a JScrollPane as part of my GUI. I've set up an AncestorListener where I respond to the ancestorAdded event. In the event, I would like to automatically expand as many visible nodes as possible. By visible, I mean that I want to expand as many nodes as possible such that the total height of the tree does not exceed the maximum height of the JScrollPane's viewport. If my viewport had 500px of visible space, I want the tree expanded until the total expanded height is less than or equal to 500px.

I've tried to achieve this a few different ways. It boils down to this: I can't seem to retrieve an updated height for the tree after programatical开发者_开发问答ly expanding a node. How can I recalculate the height of the tree? tree.getHeight(), for example, always returns the same value even after I expand a node. Invoking tree.invalidate() between calculations didn't help either.


How can I recalculate the height of the tree?

Maybe getVisibleRowCount()*getRowHeight() will give you the correct size.

The documentation for getRowHeight() states that it may return <= 0 and leave the row height up to the renderer, which may be the case for you as you're using a JTreeTable.

0

精彩评论

暂无评论...
验证码 换一张
取 消