LocoMH
LocoMH

Reputation: 95

Change Button textFill property in javafx

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

Upvotes: 0

Views: 20179

Answers (1)

DeOldSax
DeOldSax

Reputation: 489

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.

Upvotes: 8

Related Questions