1

I currently have a scrollpane that is included in an HBox Node and set with HBox.setHgrow(scrollpane, Priority.SOMETIMES);

Also, I have manually set the dimensions of this scrollpane through using:

scrollpane.setPrefWidth(w);
scrollpane.setMaxWidth(mxw);
scrollpane.setMinWidth(mnw);

Having all this set, the pane is displayed as required in its parent node, and when no other nodes are within the parent HBox, the scrollpane occupies the whole space.

When I attempt to get the width of this scrollpane, I am unable to get the calculated with, and all the values seem to return 0; I have attempted the following methods in order to obtain the real with:

scrollpane.getWidth(); // returns 0
scrollpane.getLayoutBounds.getWidth(); // returns 0
scrollpane.boundsInLocalProperty().get().getWidth; // returns 0
scrollpane.boundsInParentProperty().get().getWidth; // returns 0
scrollpane.widthProperty().get(); // returns 0
scrollpane.layoutBoundsProperty().get().getWidth(); // returns 0

I have also used scenicView v8.0.0 to check the actual value of the width (which is different from prefWidth), but I cannot seem to recover this value anywhere from the available methods on Scrollpane.

I am not quite sure what I am missing here.

Any help solving this would be greatly appreciated.

autronix
  • 2,885
  • 2
  • 22
  • 28
  • See also: [Get the height of a node in JavaFX (generate a layout pass)](http://stackoverflow.com/questions/26152642/get-the-height-of-a-node-in-javafx-generate-a-layout-pass) – jewelsea Oct 08 '15 at 20:37

1 Answers1

2

After doing some further research I discovered that the reason why all width values were set to 0 were simply because I was attempting to perform the calculations in the constructor of the class that was using the ScrollPane.

More specifically, at the constructor level, the scollpane is not yet rendered and therefore has no dimensional information in its properties, hence, no width value.

Performing the scrollpane.getWidth() after the rendering has happened works and provides the right information.

autronix
  • 2,885
  • 2
  • 22
  • 28
  • 2
    You can also bind to these properties or register listeners with them, e.g. `someDoubleProperty.bind(scrollPane.widthProperty())` etc. That code can be executed before the scroll pane is rendered, and the updates will happen when its width changes. – James_D Oct 08 '15 at 19:48