Just put the if(...)
clause inside the handle(...)
method...
errorLimit.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
if (newValue.trim().length() > 0) {
int enteredValue = Integer.parseInt(newValue);
if (enteredValue <1 || enteredValue >25000) {
errorLimit.setStyle("-fx-text-fill: red");
Dialogs.flash(errorLimit, "The error limit can not be zero or blank or greater than 25,000");
} else {
errorLimit.setStyle("-fx-text-fill: black");
}
}
}
});
errorLimit.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
if (errorLimit.getStyle().equals("-fx-text-fill: red")) {
Dialogs.flash(errorLimit, "The error limit can not be zero or blank or greater than 25,000");
}
}
});
I would strongly suggest creating a BooleanProperty
to indicate if the field is valid or not, instead of checking the value of the style; but that's a different issue I guess.