Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to change the colour of JavaFx Tab header's background

3081639Nov 21 2018 — edited Nov 24 2018

Is there a way to change the background of a Tab programmatically? I know I can use CSS to change it but my in user interface I want the user to know that something has happened on a tab that may be open when the external event arrives. Hence the need to change the tab header's background. Ideally I hope I can run an animation and flash the selected tab.

Comments

3081639

Okay I worked out how to change the background by calling setStyle("-fx-background-color: red") on the tab. However I can't work how to change the text colour. I have tried adding -fx-text-fill: white to the string in the style but the text stays black. Does anyone have any idea on how to change the text colour programatically in JavaFx 8?

jim_b_o

The Tab has a Label and that is where you need to set the text color.  In my case I do this dynamically by adding/removing a style class in code depending on a boolean isDirty value:

if (isDirty) {

                tab.getStyleClass().add("dirty");

} else {

                tab.getStyleClass().remove("dirty");

}

and the the following CSS:

.tab.dirty .tab-label {

     -fx-text-fill: orange;

}

Note the .tab-label is required so that we set the color of the text in the Label on the Tab and the .dirty selector is the style-class I'm adding/removing so that the color only changes from the default when I want it to.

1 - 2

Post Details

Added on Nov 21 2018
2 comments
4,757 views