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
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
 
public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
		try {
			/* Create a button. */
			Button btn = new Button();
			btn.setText("Login.");
			btn.setOnAction(new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent event) {
					System.out.println("Hello World!");
				}
			});
 
			/* Create a heading for your form. */
			Text sceneTitle = new Text("Welcome");
			sceneTitle.setFont(Font.font("Kalinga", FontWeight.NORMAL, 20));
 
			/* Create a flexible layout. */
			GridPane root = new GridPane();
			root.setAlignment(Pos.CENTER);
			root.setHgap(10.);
			root.setVgap(10.);
			root.setPadding(new Insets(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);
 
			Scene scene = new Scene(root, 400, 325);
			scene.getStylesheets().add(
					getClass().getResource("application.css").toExternalForm());
 
			primaryStage.setScene(scene);
			primaryStage.setTitle("FileRex");
			primaryStage.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	public static void main(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.

http://docs.oracle.com/javase/8/javafx/get-started-tutorial/form.htm