0

I need to dynamically retrieve the width of the left part of my JavaFX-BorderPane. Here is the code-snippet, but both methods I use always return 0 although left component is laid out at this point of time...

double leftWidth = ((VBox) this.getLeft()).getWidth();

same with:

double leftWidth = ((VBox) this.getLeft()).getLayoutBounds().getWidth();

How to do this properly?

Here a mcve:

public class BorderPaneExample extends Application {

@Override
public void start(Stage primaryStage) {

    Button btn = new Button();
    btn.setText("Left side of BorderPane");
    BorderPane root = new BorderPane();
    root.setLeft(btn);
    Scene scene = new Scene(root, 640, 480);
    primaryStage.setScene(scene);

    primaryStage.show();

    // ... later on retrieve the width of the left side:
    System.out.println(root.getLeft().getLayoutBounds().getWidth());
}

public static void main(String[] args) {
    launch(args);
}}
4

1 回答 1

0

stage.show()很可能你在发生之前就给他们打电话了。

在显示窗口之前,不会初始化边界。移动它们以便稍后计算,或者尝试将它们包装成Platform.runLater()一种解决方法。

于 2020-01-15T10:24:14.817 回答