Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: FXML: How to make the child to extend its size to fit the parent pane?

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.

enter image description here

like image 390
Dean Chiu Avatar asked Mar 08 '14 15:03

Dean Chiu


People also ask

How do I increase the size of the GridPane in JavaFX?

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.

How do I set margins in JavaFX?

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));

What is anchor pane?

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.


2 Answers

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> 
like image 174
Sascha B Avatar answered Sep 25 '22 02:09

Sascha B


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()); 
like image 38
EthanP Avatar answered Sep 23 '22 02:09

EthanP