JavaFX | How to set padding between nodes of a GridPane

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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 
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.