1

I've been trying to determine a TreeView's preferred width but all I get when I call getPrefWidth() is -1. Is there a reason for that? For me the preferred width would be the width of the widest cell. That value must be stored somewhere since scrollbars are displayed when the TreeView doesn't have enough space to show all of it's content.

DeLoreanDriver
  • 205
  • 1
  • 10
  • 1
    You're getting the value [`Region.USE_COMPUTED_SIZE`](https://docs.oracle.com/javase/9/docs/api/constant-values.html#javafx.scene.layout.Region.USE_COMPUTED_SIZE) (which basically means the tree view's preferred width is not hard-coded, but is computed from the preferred widths of its skin's child nodes). There's usually no reason to know the preferred width at any given time, but if you really need it try calling `prefWidth(-1)` (or perhaps `prefWidth(prefHeight(-1))`). – James_D Nov 29 '17 at 14:53
  • "That value must be stored somewhere": I don't think it is, actually, I think it's computed when needed during a layout pass. – James_D Nov 29 '17 at 14:56
  • I tried calling `prefWidth(-1)`. It gave me something but it was not the value I need. Internally the Skin seems to get the width by calling `getWidth()` on `VirtualFlow>`. I tried to access that using reflection but I failed because I am developing in Java 9. – DeLoreanDriver Nov 29 '17 at 18:24
  • You could navigate the scene graph using CSS lookups, but imho that will lead to madness... Why do you need this? Perhaps there is another solution entirely to what you are trying to do. – James_D Nov 29 '17 at 18:33
  • I am using a `TreeView` in a `SplitPane`. Right now the SplitPane makes the TreeView as large as possible since no maxWidth has been set. I don't want to set it to any random value because these things can vary so much depending on the monitor you use. The "prefWidth" I've been trying to get is the maxWidth I want to set for the TreeView. – DeLoreanDriver Nov 29 '17 at 18:51
  • Doesn't `setMaxWidth(Region.USE_COMPUTED_SIZE)`, or `setMaxWidth(Region.USE_PREF_SIZE)` work? (Also, make sure the other components in the split pane have max widths that allow them to expand.) – James_D Nov 29 '17 at 18:52
  • `setMaxWidth(Region.USE_COMPUTED_SIZE)` was what it was previously set to. That didn't help. I tried using `setMaxWidth(Region.USE_PREF_SIZE)`. That was a lot closer but too small. I am using a custom TreeCell. I store all my custom stuff in the `graphic` variable. Maybe that's the problem? – DeLoreanDriver Nov 29 '17 at 19:07

0 Answers0