Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set padding between columns of a JavaFX GridPane?

Tags:

java

javafx

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

like image 703
Ali Avatar asked Dec 08 '13 13:12

Ali


People also ask

How do I set JavaFX padding?

To set a different padding value for each edge, call it like this:. hbox. setPadding(new Insets(20, 10, 20, 10)); In this example, the top and bottom padding is set to 20 and the right and left padding is set to 10.

How do I add a column to a GridPane in JavaFX?

This feature exists in the JavaFX Scene Builder by right clicking on the GridPane and selecting GridPane, then it will show a list of options: Add Row Above, Add Row Below, Add Column After, Add Column Before.

How does GridPane work in JavaFX?

GridPane lays out its children within a flexible grid of rows and columns. If a border and/or padding is set, then its content will be layed out within those insets. A child may be placed anywhere within the grid and may span multiple rows/columns.

How do I declare a GridPane in JavaFX?

To make a JavaFX GridPane visible you must add it to the JavaFX scene graph. To do so you must add the GridPane instance to a Scene object, or add the GridPane to a layout component which is added to a Scene object.


1 Answers

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) 
like image 89
assylias Avatar answered Sep 18 '22 17:09

assylias