How to set the distance to the border in a GridPane?

1.7k views Asked by At

I am unable to find a way to set the distance to the border in a GridPane. Now, the text starts immediately after the border stops, which is not very nice for the design. I could give all the children a padding, but I was thinking there must be a shorter way to do this (just some property of the GridPane).

Unfortunately, I was not able to find this anywhere online. I prefer a solution which uses FXML or CSS (preferably CSS), but if this must be done with some Java code, it is not a problem.

I have already tried:

  • Setting the margin of the GridPane in the CSS: #someGridPane { -fx-border-insets: 5; }, but these insets are outside the border (and I want them inside).
  • Setting padding on all of the child elements, which is not as efficient as I had hoped).

So the question is: How to set the distance to the border in a GridPane?

Notes:

  1. I am using JavaFX8
  2. If the solution is somewhere online already, please let me know. (I searched for it for some time now)
2

There are 2 answers

3
brian On BEST ANSWER

Depending on what's in your grid pane you could cheat a bit.

GridPane > Text  {-fx-translate-x : 5;}
GridPane > Label {-fx-label-padding: 0 0 0 5;}

Of course, add some style class names, but you get the idea.

0
xeruf On

There is a simpler, more natural solution:

GridPane { 
    -fx-padding: 5; 
    -fx-hgap: 5; 
    -fx-vgap: 5; 
}

It does the same as using GridPane.setHgap(5) etc, but with CSS.