Unable to resize properly nested JavaFX panel

I have the current situation: the main windows with the main BorderPane . In the center of it is AnchorPane with some objects inside. I want to distribute objects equally inside the height of the panel, even if the size of the area changes.

The problem I am facing is that all things work when resizing increases the height of the panel. When I reduce the size of the window, the height of the panel continues to grow.

I reproduced the error in this example application using a simple line (in my application I also have a line):

 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.shape.Line; import javafx.stage.Stage; public class ResizeProblem extends Application { @Override public void start(Stage primaryStage) { BorderPane root = new BorderPane(); AnchorPane inner = new AnchorPane(); Line line = new Line(); // comment from here... root.setCenter(inner); Pane pane = inner; // ...to here and uncomment next to make things work //Pane pane = root; line.startXProperty().bind(pane.widthProperty().divide(2)); line.endXProperty().bind(pane.widthProperty().divide(2)); line.setStartY(0.); line.endYProperty().bind(pane.heightProperty()); pane.heightProperty().addListener((obs, ov, nv) -> System.out.println("ov: " + ov + " nv: " + nv)); pane.getChildren().add(line); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Resize Problem"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } 

I saw that, avoiding nesting, everything worked as expected.

What is wrong with that?

Please help me find the right direction.

+6
source share
1 answer

This is my explanation.

BorderPane does not resize its children to its Bounds layout, it is sloppy, the same applies to AnchorPane and even Pane , when you have one nesting, the endpoint of your line matches the Pane height, so it automatically obeys what- or appreciated by her, but if you invest AnchorPane in BorderPane , who will take care of the children? BorderPane and AnchorPane are a bad marriage if you do not want to put them in a marriage contract, where you manually check each height and alignment, so the height of BorderPane increases and AnchorPane does the same, and your match matches the parent. In addition, the static inner link is equal to Pane , and with Pane you need to manually place your children in Node , Pane doesn’t do this for you, so using Pane means your children will be street children who are wondering.

Hope this is helpful

+1
source

Source: https://habr.com/ru/post/985277/


All Articles