JavaFX Panel inside Panel auto resizing
I was designing a GUI in SceneBuilder, trying to make the main container adapt to whatever the window size is. It should always be 100% wide.
This is where you can set these values in SceneBuilder:
Toggling the dotted/red lines will actually just add/remove the attributes that Korki posted in his solution (AnchorPane.topAnchor etc.).
No need to cede.
just select pane ,right click then select Fit to parent.
It will automatically resize pane to anchor pane size.
After hours of searching and testing finally got it just after posting the question!
You can use the "AnchorPane.topAnchor, AnchorPane.bottomAnchor, AnchorPane.leftAnchor, AnchorPane.rightAnchor" fxml commands with the value "0.0" to fit/stretch/align the child elements inside a AnchorPane. So, these commands tell to child element to follow its parent while resizing.
My updated code Main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="anchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="app.MainController">
<!--<StackPane fx:id="stackPane" ></StackPane>--> <!-- replace with the following -->
<StackPane fx:id="stackPane" AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" ></StackPane>
</AnchorPane>
Here is the result:
For api documentation: http://docs.oracle.com/javafx/2/api/javafx/scene/layout/AnchorPane.html
Also see here
@FXML
private void mnuUserLevel_onClick(ActionEvent event) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("DBedit.fxml"));
loader.setController(new DBeditEntityUserlevel());
try {
Node n = (Node)loader.load();
AnchorPane.setTopAnchor(n, 0.0);
AnchorPane.setRightAnchor(n, 0.0);
AnchorPane.setLeftAnchor(n, 0.0);
AnchorPane.setBottomAnchor(n, 0.0);
mainContent.getChildren().setAll(n);
} catch (IOException e){
System.out.println(e.getMessage());
}
}
The scenario is to load a child fxml into parent AnchorPane. To make the child to stretch in accords to its parent use AnChorPane.setxxxAnchor command.