Reputation: 75
I've the current situation: a main windows with a main BorderPane
. At the center of it I have an AnchorPane
with some objects inside. I want to distribute the objects equally inside the height of the pane, even if the pane is resized.
The problem I'm falled in is that all things works when the resize increase the height of the pane. When I decrease the size of the window, the pane's height continue to increase.
I have reproduced the error in this example application using a simple line (in my application I've also a line like this):
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've seen that avoiding nesting, everything works as expected.
What's wrong with that?
Please help me find the right direction.
Upvotes: 6
Views: 1316
Reputation: 10859
This is my explanation ..
BorderPane
does not resize its children to its layout Bounds
, it careless, same goes to AnchorPane
and even Pane
, when you have one nesting, your line ending point is matched with the Pane
's height, so automatically it obeys what ever value is being restricted on it, but if you nest an AnchorPane
in a BorderPane
who is going to take care of the kids? BorderPane
and AnchorPane
is a bad marriage, unless you want to put them in a contract marriage where you manually check each individual's height and alignment, so the BorderPane
's height increases and the AnchorPane
does the same, and your respect matching is to the parent. Also your static
reference of inner
is a Pane
, and with Pane
you have to manually position your children in your Node
, Pane
does not do that for you so using Pane
means your children will be street kids wondering around.
Hope it helpful
Upvotes: 2