Welcome to Stackoverflow Labib, regarding the splitting up:
You can have a root FXML that contains for example just a GridPane(or sth else, but I'll continue on a GridPane premise), now you set up the Grid (without content) in that root FXML file, if you already have a finished GUI, you should have a clear idea on how big the grid-Cells need to be. In your main-Class you have:
Main Class that extends Application and launches GUI
public class GUI extends Application {
private Stage stage;
private GridPane rootGrid;
@Override
public void start(Stage stage) {
this.stage = stage;
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(WindowRoot.class.getResource("RootGrid.fxml"));
rootGrid = (GridPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootGrid);
stage.setTitle("Test");
stage.show();
//fill the Grid
setupGrid();
}
Now you have rootGrid
as class attribute which is just a grid with empty rows/columns as set up in the RootGrid.fxml
, can be as simple as:
FXML example, two rows, two columns, no content
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141">
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints minHeight="10.0" prefHeight="30.0" />
</rowConstraints>
<columnConstraints>
<ColumnConstraints minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
</GridPane>
Now you create FXML documents for every Cell you want to fill and in that FXML you have one AnchorPane with the contents of that Cell. Then you add it to the Grid as part of the start
method. Let's say you want in that simple example a menubar. Create a FXML for the menu:
simple menu fxml example:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
And add it to the grid as part of the start
method (but probably extract it into a method that you call in start, like setupGrid()
:
Setting up Grid, called in start(Stage stage)
private void setupGrid() {
// Add Menubar
FXMLLoader loader = new FXMLLoader();
loader.setLocation(WindowRoot.class.getResource("menubar.fxml"));
AnchorPane anchor = (AnchorPane) loader.load();
//put it in first column, first row
GridPane.setConstraints(anchor, 0, 0);
//optional, let it span both/all columns
GridPane.setRowSpan(anchor, 2);
rootGrid.getChildren().add(anchor);
You'll need try/except IO blocks, left them out for better readability. For other "Root-Panes" you'll need other setup-methods, with a borderPane for example you'd have things like rootGrid.setTop(anchor)
.
Hope it helps. Have fun.