Hello, I am studying javafx, and I need to write so that when adding a "node" to the "scene", the window itself increases to the desired size. ( window.setHeight(window.getHeight() + myNode.getBoundsInLocal().getHeight()); ), but myNode.getBoundsInLocal().getHeight() 0.0.

All code:

 MyNode myNode = new RootFinderResult(findersNames); vBox.getChildren().add(myNode); window.setHeight(window.getHeight() + myNode.getBoundsInLocal().getHeight()); 

What am I doing wrong?

    1 answer 1

    In AWT , you can add the ComponentListener interface to JPanel , and then override the componentResized() method to get the new widths and heights. Try to register the listener ComponentAdapter for example in the following way:

     addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { System.out.println("Новая ширина: " + getWidth() + " Новая высота: " + getHeight()); } }); 

    In JavaFX probably won't be able to do this, perhaps the second option will suit you:

     scene.widthProperty().addListener(new ChangeListener<Number>() { @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) { System.out.println("Ширина: " + newSceneWidth); } }); scene.heightProperty().addListener(new ChangeListener<Number>() { @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) { System.out.println("Высота: " + newSceneHeight); } }); 
    • Thanks for the answer. But better with lambda (myNode.heightProperty (). AddListener ((observable, oldValue, newValue) -> {};) - Andrey
    • @ ScreamTV5 is not what, I hope that helped answer. Lambda or not, this is a matter of taste. - dev3java