In order to add a grid layout to an JavaFX Application window, you have to instantiate the class GridPane.
An object of this class is tehn added to a Scene of an Applications Stage.
I have to admit that JavaFX looks much nicer than Swing even without applying any customizing using CSS.
Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
importjavafx.application.Application;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.geometry.Insets;importjavafx.geometry.Pos;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.GridPane;importjavafx.scene.text.Font;importjavafx.scene.text.FontWeight;importjavafx.scene.text.Text;importjavafx.stage.Stage;publicclassMainextendsApplication{@Overridepublicvoidstart(StageprimaryStage){try{/* Create a button. */Buttonbtn=newButton();btn.setText("Login.");btn.setOnAction(newEventHandler<ActionEvent>(){@Overridepublicvoidhandle(ActionEventevent){System.out.println("Hello World!");}});/* Create a heading for your form. */TextsceneTitle=newText("Welcome");sceneTitle.setFont(Font.font("Kalinga",FontWeight.NORMAL,20));/* Create a flexible layout. */GridPaneroot=newGridPane();root.setAlignment(Pos.CENTER);root.setHgap(10.);root.setVgap(10.);root.setPadding(newInsets(25,25,25,25));/* Add a text to a layout. */root.add(sceneTitle,0,0,2,1);/* Add a button to a layout. */root.add(btn,2,2);Scenescene=newScene(root,400,325);scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());primaryStage.setScene(scene);primaryStage.setTitle("FileRex");primaryStage.show();}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){launch(args);}}
The fucntion setPadding of the GridPane is setting the space between the components and the layout GridPane. The spaces are set from left to right for top, right, bottom and left sides of a GridPane.
Methods setHgap and setVgap are determining the space between nodes within a GridPane.
The example above is taken from the Oracle Java Tutorials.