简体   繁体   中英

Javafx HBox Hgrow(SOMETIMES) child scrollpane node how to get width?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM