I have managed to load a child fxml(sub UI) under a parent fxml (mainMenu UI). I have created an AnchorPane with id "mainContent". This pane is bound to 4 sides and changes in accords to the stage.
The child window will be loaded into the "mainContent" anchorpane. However, I can't figure out how to make the child to change along with its parent "mainContent".
My child UI is called like this.
@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(); mainContent.getChildren().add(n); } catch (IOException e){ System.out.println(e.getMessage()); } }
To further illustrate my question, please see my snap shot. The red square is the child. The yellow square is the "mainContent" AnchorPane of the MainMenu parent.
getColumnConstraints(). add(new ColumnConstraints(200)); By default the GridPane will resize rows/columns to their preferred sizes even if the gridpane is resized larger than its preferred size. To support dynamic column/row sizes, both contstaints class provides three property: min size, max size and preferred size.
You can set the margin for child nodes of a JavaFX VBox using the static setMargin() method. Here is an example of setting the margin around a JavaFX Button using the setMargin() method: Button button = new Button("Button 1"); VBox vbox = new VBox(button); VBox. setMargin(button, new Insets(10, 10, 10, 10));
AnchorPane allows the edges of child nodes to be anchored to an offset from the anchor pane's edges. If the anchor pane has a border and/or padding set, the offsets will be measured from the inside edge of those insets.
If you set the static
methods setTopAnchor( child, value )
, setBottomAnchor( ... )
, setLeftAnchor( ... )
, setRightAnchor( ... )
of class AnchorPane
to 0.0, the child Node
will get stretched to the full extend of the parent AnchorPane
.
Documentation Link: AnchorPane
edit: in the documentation link you can also see how you can set these values in your java code.
FXML example:
<AnchorPane fx:id="mainContent" ...> <StackPane fx:id="subPane" AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" ></StackPane> </AnchorPane>
If you have a Region
, which is a subclass of Node
that includes Axis
, Chart
, Control
, and Pane
(this probably includes anything you might want to load), you can bind the child's size to the space in the parent similar to how they did here. Now any future adjustments of the parent's size will be reflected in the child.
Region n = (Region)loader.load(); mainContent.getChildren().add(n); n.prefWidthProperty().bind(mainContent.widthProperty()); n.prefHeightProperty().bind(mainContent.heightProperty());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With