Open In App

JavaFX | How to set padding between nodes of a GridPane

Improve
Improve
Like Article
Like
Save
Share
Report

In most cases, we look forward to seeing objects organized and well arranged, especially when we want efficiency
while 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 GridPane
import 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 GridPane
import 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:



Last Updated : 06 Jun, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads