MKatana
MKatana

Reputation: 159

How do I get normal JavaFX text field after changing text field color?

I am trying to get a normal text field after I changed the color. Salary and Wage is the red text field I change it to. Investment income is my attempt at making it go back to normal and Total income is the text field I would like to get after I changed the color to red.

This is the code I am using for the color change. Any help would be much appreciated.

tfTotalPaymentOverLife.textProperty().addListener((ob, oldValue, newValue) -> {
        if (isNumeric(newValue)) {
            tfTotalPaymentOverLife.setStyle("-fx-background-color: transparent;");
        } else {
            tfTotalPaymentOverLife.setStyle("-fx-background-color: red;");

        }
    });

Hello

Upvotes: 1

Views: 2203

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36722

You can use the -fx-control-inner-background css to paint the inner background of the textfield.

You can set the desired background color to this property or go back to the default color by using setStyle("")

Code :

textField.textProperty().addListener((ob, oldValue, newValue) -> {
     if (isNumeric(newValue)) {
         textField.setStyle("");
     } else {
         textField.setStyle("-fx-control-inner-background: red");
     }
});

Upvotes: 4

Related Questions