Change Button textFill property in javafx


Question

Using javafx, I have a button which in the css properties has the textFill set to white. Then during runtime it needs to be changed, which happens with this code:

import javafx.scene.control.Button;
import javafx.scene.paint.Color;

public class QAButton extends Button {

    public QAButton(String text) {
        this.textProperty().set(text);
    }

    public void setAnswerVisible(Boolean vis) {

        if (vis) this.setTextFill(Color.GREEN);
        else this.setTextFill(Color.BLUE);
    }

}

However it doesn't do anything, the textFill will stay white. What can I do to change it during runtime? Thank you.

Edit: I should add the css code:

.button {
    -fx-text-fill: white;
}

And the button's id is set to

#question {
    -fx-background-color: darkblue;
    -fx-background-radius: 0;
    -fx-max-width: 2000px;
    -fx-max-height: 200px;
    -fx-min-height: 80px;
}
1
0
9/28/2014 10:50:18 AM

Accepted Answer

Extraction from JavaFX CSS Reference (second section):

"The implementation allows designers to style an application by using style sheets to override property values set from code."

The setStyle API can fix your problem.

Example:

this.setStyle("-fx-text-fill: green");

Now all attributes in the style class will be overwritten. If this is a problem, look here.

4
5/23/2017 12:13:20 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon