0

How can I share settings across multiple classes? I need to apply certain styles to my tableview cells (depending on the enum value displayed) and would prefer not to repeat the values as below -

.A { 
    -fx-background-color: red;
}

.B { 
    -fx-background-color: red;
}
2
  • 1
    Just add the class name to the stylesheet of the cell. Commented Nov 13, 2015 at 5:39
  • I have created one style class for each java enum present in my model. Some of these classes have the same settings. Hence my aim to reduce css duplication. This way I can set the string value of an enum as the style class. Commented Nov 15, 2015 at 3:39

2 Answers 2

3

You can apply rules to multiple selectors with

.A, .B {
    -fx-background-color: red;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should read the Skinning JavaFX Applications with CSS tutorial and the JavaFX CSS Reference Guide.

Excerpt:

You can create a class style by adding a definition for it to your style sheet. Example 5 defines a new style in controlStyle1.css called .button1.

Example 5 Define a New Style

.button1{
    -fx-text-fill: #006464;
    -fx-background-color: #DFB951;
    -fx-border-radius: 20;
    -fx-background-radius: 20;
    -fx-padding: 5;
}

To assign this class style to a node, use the getStyleClass().add() sequence of methods. Example 6 shows the .button1 style assigned to the Accept button.

Button buttonAccept = new Button("Accept");
buttonAccept.getStyleClass().add("button1");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.