How to set padding between columns of a JavaFX GridPane?


Question

I'm trying to show a 2-column grid in a javaFx program. This is how I'm setting up the grid:

    GridPane grid = new GridPane();

    ColumnConstraints column1 = new ColumnConstraints();
    column1.setPercentWidth(50);
    ColumnConstraints column2 = new ColumnConstraints();
    column2.setPercentWidth(50);
    grid.getColumnConstraints().addAll(column1, column2); 

Here's the problem. I want to show a small space between where one column ends, and the other starts. However, the columns are showing up as glued to one another.

Here's a screenshot:

screenshot

Here each column contains the 'item name' and 'process, edit, delete' buttons.

You can see how the columns are glued together. I want them to instead have some space between them.

How can I solve this?

The hierarchy of my overall UI is this:

Scene > ScrollPane > BorderPane > Vbox (Center) > GridPane

1
25
12/8/2013 1:36:26 PM

Accepted Answer

For better appearance you can use a mix of:

grid.setHgap(10); //horizontal gap in pixels => that's what you are asking for
grid.setVgap(10); //vertical gap in pixels
grid.setPadding(new Insets(10, 10, 10, 10)); //margins around the whole grid
                                             //(top/right/bottom/left)
51
12/8/2013 1:43:54 PM

In addition to assylias's answer, you can configure the margin for any given node within the grid:

GridPane.setMargin(node, new Insets(5, 10, 5, 10));

GridPane.setMargin javadoc:

Sets the margin for the child when contained by a gridpane. If set, the gridpane will lay it out with the margin space around it. Setting the value to null will remove the constraint.

Eventually (post Java 8), JavaFX may allow you to set a margin around any node, not just those in a GridPane.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon