Grab Scrollpane size when set by anchoring - Javafx

Asked

Viewed 112 times

3

I have a Scrollpane anchored on the 4 sides of an Anchorpane so that when resizing the screen, the scroll also resizes. So I don’t need to specify a fixed size. Until then it works as I want.

The problem is that this way the methods getPrefWidth(), getWidth(), getMinWidth(), getMaxWidth() do not return the current scroll size.

Does anyone know how to get the size of the Scrollpane by defining its size by anchoring and not by hair sets size-defining?

2 answers

2


You can use the function getBoundsInParent or getBoundsInLocal. They are functions that calculate node dimensions based on their coordinates. Then use the getHeight() and getWidth() methods (or others you deem appropriate).

Example:

AnchorPane parent = new AnchorPane();
ScrollPane child = new ScrollPane();

AnchorPane.setTopAnchor(child, 10.0);
AnchorPane.setBottomAnchor(child, 10.0);
AnchorPane.setLeftAnchor(child, 10.0);
AnchorPane.setRightAnchor(child, 10.0);

parent.getChildren().add(child);

// Painel principal
Scene scene = new Scene(parent, 200, 200); 
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();

System.out.println("Height: " + child.getBoundsInParent().getHeight()); // 180
System.out.println("Width: " + child.getBoundsInParent().getWidth()); //180

References: Get the Dimension of a Node

  • Thanks, Gustavo! It worked too well

0

For the vertical bar:

jScrollPane1.getVerticalScrollBar().getMaximum()

or for the horizontal bar:

jScrollPane1.getHorizontalScrollBar().getMaximum()
  • Hi Rocigno. Actually the methods you suggested are for the scroll of the swing graphical library. In Javafx it is different :\

Browser other questions tagged

You are not signed in. Login or sign up in order to post.