Java Reference
In-Depth Information
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.getChildren().add(menuBar);
14-7. Adding Components to a Layout
Problem
You want to add UI components to a layout similar to a grid type layout for easy place-
ment.
Solution
Use JavaFX's javafx.scene.layout.GridPane class. This source code imple-
ments a simple UI form containing first and last name field controls and using the grid
pane layout node ( javafx.scene.layout.GridPane ):
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(5));
gridpane.setHgap(5);
gridpane.setVgap(5);
Label fNameLbl = new Label(“First Name”);
TextField fNameFld = new TextField();
Label lNameLbl = new Label(“First Name”);
TextField lNameFld = new TextField();
Button saveButt = new Button(“Save”);
// First name label
GridPane.setHalignment(fNameLbl, HPos.RIGHT);
gridpane.add(fNameLbl, 0, 0);
// Last name label
GridPane.setHalignment(lNameLbl, HPos.RIGHT);
gridpane.add(lNameLbl, 0, 1);
Search WWH ::




Custom Search