首页 > 解决方案 > Is there an event when for example to detect when a text field loses focus?

问题描述

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!");
            }
        }
    });

标签: javafx

解决方案


推荐阅读