Darth Ninja
Darth Ninja

Reputation: 1039

Sharing JavaFX css values across multiple classes

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;
}

Upvotes: 0

Views: 2810

Answers (2)

James_D
James_D

Reputation: 209358

You can apply rules to multiple selectors with

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

Upvotes: 3

Roland
Roland

Reputation: 18415

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");

Upvotes: 0

Related Questions