Question

Trying to control the display of a message based on the value in a TextField. I have a fade away timed message display based on certain value input in the TextField box. Like to have a mouse hover over to display the same message while the input value is still invalid. Looks like the code I have below would still show the message even after the input value has beed changed to be valid.

...
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");

                  if (errorLimit.getStyle().equals("-fx-text-fill: red")) {
                       errorLimit.setOnMouseEntered(new EventHandler<MouseEvent>() {

                          @Override
                          public void handle(MouseEvent t) {
                               Dialogs.flash(errorLimit, "The error limit can not be zero or blank or greater than 25,000");
                          }

                       });
                  }
              } else {

                  errorLimit.setStyle("-fx-text-fill: black");
              }

          }
      }
    });
 ...

Any idea how I can do this? Thanks!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top