JavaFX | How to set padding between nodes of a GridPane - GeeksforGeeks

06 Jun, 2019
In most cases, we look forward to seeing objects organized and well arranged, especially when we want efficiencywhile using them. In the case of object nodes of JavaFX GridPane, there will always be a need to make these objects look smart and organized.
Example: Let us see these four buttons on GridPane
// Java Program to show the four // buttons on the GridPaneimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.GridPane;import javafx.stage.Stage;  public class GridpaneExamplePadding extends Application {      public void start(Stage primaryStage)    {        primaryStage.setTitle("GridPane Example");          // creating buttons        Button button1 = new Button("Button A");        Button button2 = new Button("Button B");        Button button3 = new Button("Button C");        Button button4 = new Button("Button D");          // creating Gridpane object        GridPane gridPane = new GridPane();          // adding buttons to the Gridpane        gridPane.add(button1, 0, 0);        gridPane.add(button2, 1, 0);        gridPane.add(button3, 1, 1);        gridPane.add(button4, 0, 1);          // Adding Gridpane to the scene         // and showing the primary stage        Scene scene = new Scene(gridPane, 200, 100);        primaryStage.setScene(scene);        primaryStage.show();    }      // main to run our javaFx application    public static void main(String[] args)    {        Application.launch(args);    }}
Output:
You may need to add some spacing between buttons to make things look nice.
Adding space between columns of the Gridpane:
gridpane.setHgap(5) // set gap in pixels 

Adding space between rows of the Gridpane:
gridpane.setVgap(5) // set gap in pixels 

// Java program to add some spacing // between the buttons of the GridPaneimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.GridPane;import javafx.stage.Stage;  public class GridpaneExamplePadding extends Application {      public void start(Stage primaryStage)    {        primaryStage.setTitle("GridPane Example");          // creating buttons        Button button1 = new Button("Button A");        Button button2 = new Button("Button B");        Button button3 = new Button("Button C");        Button button4 = new Button("Button D");          // creating Gridpane object        GridPane gridPane = new GridPane();          // adding buttons to the Gridpane        gridPane.add(button1, 0, 0);        gridPane.add(button2, 1, 0);        gridPane.add(button3, 1, 1);        gridPane.add(button4, 0, 1);          // spacing the buttons        gridPane.setHgap(10);        gridPane.setVgap(10);          // Adding Gridpane to the scene        // and showing the primary stage        Scene scene = new Scene(gridPane, 200, 100);        primaryStage.setScene(scene);        primaryStage.show();    }      // main to run our javaFx application    public static void main(String[] args)    {        Application.launch(args);    }}
Output:
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
Set in Java
Multithreading in Java
Collections in Java
Queue Interface In Java
Constructors in Java
Initializing a List in Java
Exceptions in Java
Inheritance in Java