9

I've used html+css quite a lot but I'm completely new to javafx+css. So this will be a newbie question but I can't find the answer anywhere.

I have a large GridPane full of Labels (besides others). I can set padding for all these Labels like:

GridPane.containerLevel02 Label {
    -fx-padding: 0 0 0 5;
}

This works. I want some of the labels to be more indented so I create the "leftIndent" class for them:

GridPane.containerLevel02 Label.leftIndent {
    -fx-padding: 0 0 0 20;
}

This works too.

BUT, I don't want to specify the three zeros there because if I ever decide to add top/bottom padding to Labels in general, it will not affect the "leftIndent" labels.

What I'm looking for is something like:

GridPane.containerLevel02 Label.leftIndent {
    -fx-padding-left: 20;
}

... like in "normal" CSS or ...

GridPane.containerLevel02 Label.leftIndent {
    -fx-padding: null null null 20;
}

...or anything that would let me specify left padding leaving the top, right and bottom padding untouched with whatever values they currently have.

Is that possible? THANKS!

tomorrow
  • 1,260
  • 1
  • 14
  • 26
  • Please check this http://stackoverflow.com/questions/16977100/confgure-margin-for-individual-element-via-java-fx-css – vignesh Jul 22 '16 at 14:06
  • Thanks for a quick comment but that seems to be a different problem they solve there. – tomorrow Jul 22 '16 at 14:25
  • 1
    AFAIK this is impossible without a custom skin, since it's a `StyleableProperty` and needs to be assigned from a single rule and afaik it's impossible to combine multiple variables to one `` element. – fabian Jul 22 '16 at 15:44
  • Ok, I didn't look at it that way. I guess I'm biased by the 'normal' CSS. I'll have to do some reading and testing to understand how exactly it works. Thanks for your point anyway! – tomorrow Jul 22 '16 at 16:24

1 Answers1

15

Since JavaFX does not provide a -fx-padding-left or similar, I think your easiest workaround is to use variables. For instance:

.root {
  LABEL_PADDING_TOP: 0;
  LABEL_PADDING_RIGHT: 0;
  LABEL_PADDING_BOTTOM: 0;
  LABEL_PADDING_LEFT: 5;
}

GridPane.containerLevel02 Label {
    -fx-padding: LABEL_PADDING_TOP LABEL_PADDING_RIGHT LABEL_PADDING_BOTTOM LABEL_PADDING_LEFT;
}

GridPane.containerLevel02 Label.leftIndent {
    -fx-padding: LABEL_PADDING_TOP LABEL_PADDING_RIGHT LABEL_PADDING_BOTTOM 20;
}

It still requires you to override all values which can be unwanted in certain use cases, but at least you have to specify your defaults only at one place.

Michel Jung
  • 2,966
  • 6
  • 31
  • 51
  • 4
    Not as nice as -fx-padding-left would have been but definitely good enough fo my use case! Thanks! – tomorrow Jul 25 '16 at 09:43