0

I have a swingFX GUI and a textField where the user enters a birth-date. This textField is initialized with the string "DD.MM.YYYY". After I press the mouse button, the textField is cleared and a user can enter a birth-date-string. I just want to dispay the initialized string "DD.MM.YYYY" if the user does not enter any string iside the textField. So basically, when the textField looses its focus (no cursor is blinking) I want to check the value inside the textField and if there is no character inside (stringlength = 0), I want to change the textField back to "DD.MM.YYYY".

Here is what I have so far for clearing the textField after mouse-event was fired:

textBirthDate.setOnMouseClicked(e -> {
    if(textDeathDate.getText().equals("DD.MM.YYYY")) {
        textDeathDate.clear();
    }
});

How to catch a focus-loosing-event? Is there such a listener? I dont want to use the mouse-exited-listener! Thanx for any help!

Serek
  • 35
  • 4
  • swingFX ... Is that Swing or JavaFX? – MadProgrammer Nov 26 '19 at 09:40
  • 1
    For JavaFX, see [How to Add Prompt Text to a Text Field in JavaFX](http://www.learningaboutelectronics.com/Articles/Prompt-text-in-JavaFX.php); For Swing, I'd use the SwingX libraries "prompt" support, [for example](https://stackoverflow.com/questions/22396282/how-to-set-text-like-placeholder-in-jtextfield-in-swing/22396303#22396303) – MadProgrammer Nov 26 '19 at 09:42
  • MadProgrammer, you solved my problem. I did not know about that "prompt-text-thing" at all. Thank you! – Serek Nov 26 '19 at 10:59
  • @MadProgrammer and yes, it is JavaFX and not swingFX (sorry for that). I still wonder thou, how to listen to end of focus, because I want to check this string for validation. For exampel, I want to make sure, that there is no 32.13.2019. And this check I want to perform, after the user relocates the focus to another textField. Can you understand what I mean? – Serek Nov 26 '19 at 13:07
  • Ok, so in Swing, I'd use a `InputVerifier`, in JavaFX, you can, apparently, use something [like this](https://stackoverflow.com/questions/45007773/javafx-textfield-alternative-for-swing-inputverifier) – MadProgrammer Nov 26 '19 at 20:48
  • 1
    If that still doesn't do what you want, then you can could look at using a property change listener, [for example](https://stackoverflow.com/questions/16549296/how-perform-task-on-javafx-textfield-at-onfocus-and-outfocus) – MadProgrammer Nov 26 '19 at 20:50
  • @MadProgrammer the second solution with the property change listener did solved my problem. Thanx man! – Serek Nov 27 '19 at 06:48

0 Answers0