0

I'm trying to find an solution to detect when a TextField loses focus. This is some code I have at the moment.

Is there an event just like KeyEvent or do I need to use an other method?

private void buildGui() {

    setVgap(10);
    setHgap(10);
    setPadding(new Insets(50));

    Label lblgb = new Label("Gebruikersnaam:");
    Label lblww1 = new Label("Wachtwoord:");
    Label lblww2 = new Label("Bevestig wachtwoord:");

    txfGebruikersnaam = new TextField();
    txfWachtwoord = new TextField();
    txfWachtwoord2 = new TextField();

    txfGebruikersnaam.setOnMouseExited(new javafx.event.EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            String gebr = txfGebruikersnaam.getText().trim();

            if (gebr.matches("\\s")) {
                String[] strSplit = gebr.split(" ");
                String a = strSplit[0];
                String b = strSplit[1];

                if (a.length() >= 4 && b.length() >= 8) {
                    if (!(Character.isUpperCase(a.charAt(0)) && Character.isDigit(b.charAt(b.length() - 1)))) {
                        errorAlerts("Het eerste woord moet beginnen met een hoofdletter, het 2e moet eindigen met een cijfer!");
                    }
                } else {
                    errorAlerts("Het eerste woord moet minstens 4 letters lang zijn, het 2e minstens 8!");
                }
            } else {
                errorAlerts("Uw gebruikersnaam moet uit 2 woorden bestaan!");
            }
        }
    });
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Possible duplicate of [How perform task on javaFX TextField at onfocus and outfocus?](https://stackoverflow.com/questions/16549296/how-perform-task-on-javafx-textfield-at-onfocus-and-outfocus) – Pagbo May 13 '18 at 14:10
  • 1
    There is a `Node.focused` property and a `Scene.focusOwner` property. Furthermore a `TextFormatter`'s converter is applied when the `TextField` looses focus. – fabian May 13 '18 at 14:11
  • thanks for the comment ill look into it – Java_pls_why May 15 '18 at 12:33

0 Answers0