Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set the color or texture of a tab label in JavaFX?

I have a TabPane with several Tabs. If the results of an operation in the Tab failed, I want to set the Tab Label to a Fill red or perhaps the texture to hashed (for those with color blindness). I want to reset the Color back to its default, later.

From reading some of the questions here, one can statically set it using a style sheet.

#MyTabPane .tab *.tab-label {
    -fx-text-fill: white;
}

How would one access the Tab label and set it's color/texture dynamically?

tab.setStyle("??");

ADDITIONS BY ELLTZ

How can one use inline Styles stated above to change the Paint of both the Label with style class tab-label and the Button(StackPane) also tab-close-button

code examples needed

like image 846
likejudo Avatar asked Jan 17 '13 00:01

likejudo


People also ask

How do you change the color of a label in JavaFX?

control. Label class. Just like a text node you can set the desired font to the text node in JavaFX using the setFont() method and, you can add color to it using the setFill() method.

How do I change the text in a JavaFX label?

Set Label Font You can change the font used by a JavaFX Label by calling its setFont() method. This is useful if you need to change the size of the text, or want to use a different text style.

How do I change the stage color in JavaFX?

The simplest way to set the JavaFX Scene background color or image is by invoking the Scene 's setFill() method, which can accept a color, gradient or image pattern. A more flexible way to set the background of a scene is to set the root node's background, which can accept multiple images and fills.

How do you initialize a label in JavaFX?

Syntax to Initialize JavaFX label is: Label lbl = new Label(); Here, the constructor can be of parameterized and non-parameterized, which depends on the requirement.


1 Answers

Setting the graphics and styling it did the trick for me:

Tab tabB = new Tab();
tabB.setText("");
tabPane.getTabs().add(tabB);
tabB.setStyle("-fx-border-color:red; -fx-background-color: blue;");
tabB.setGraphic(new Label("Tab B"));
tabB.getGraphic().setStyle("-fx-text-fill: #c4d8de;");
like image 52
zhujik Avatar answered Sep 20 '22 17:09

zhujik