Capture Textfield change from Javafx

Asked

Viewed 559 times

1

Have some event that captures every change that occurs in a Javafx Textfield?

2 answers

0

Add a Listener to textProperty of your Textfield:

textField.textProperty().addListener((observable, oldValue, newValue) -> {
    System.out.println("Valor antigo = " + oldValue + ". Valor novo = " + newValue);
});

0


Ask a little generic because there are several properties of Textfield that can be observed by the programmer. Looking at the documentation we have:

Properties inherited from Textinputcontrol (These are the main):

anchovy, caretPosition, Editable, font, length, promptText, redoable, selectedText, Selection, textFormatter, text, undoable

All these properties have methods with the word Property at the end (ex.: textProperty()). In addition to the above mentioned methods we have 104 properties that can be observed.

I will talk about the properties that are normally treated, what they do and how to capture the changes.

textProperty(): I believe it is the most common. This property involves the inserted/deleted text in the component.

textfield.textProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> ov, String oldValue, String newValue) {
        // oldValue = Texto anterior a edição
        // newValue = Texto atual
        System.out.println(newValue);
    }
});

// Código acima usando lambdas
text.textProperty().addListener((ObservableValue<? extends String> ov, String oldValue, String newValue) -> {
    System.out.println(newValue);
});

Obs.: We usually use the Changelistener (as the name indicates it captures changes) but there is the option to use a Invalidationlistener to notify an invalid status.

lengthProperty(): This property involves the size of the component text as the user types/deletes the content.

textfield.lengthProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> ov, Number oldValue, Number newValue) {
         if(newValue.intValue() > 10)
             System.out.println("Texto muito longo");
    }
});

// Código acima usando lambdas
textfield.lengthProperty().addListener((ObservableValue<? extends Number> ov, Number oldValue, Number newValue) -> {
   if(newValue.intValue() > 10)
       System.out.println("Texto muito longo");
});

As you may have seen, the code for capturing changes is similar across properties. However, Textfield has a particularity in this question that is Textformatter. With it you have the possibility to analyze a change before it is effective (Only for java 8u40+)

textfield.setTextFormatter(new TextFormatter<>(change ->
    {
        if (change.getControlNewText().isEmpty()) {
            return change;
        }

        String text = change.getControlNewText();
        for (int i = 0; i < text.length(); i++){
            if(!Character.isDigit(text.charAt(i))){
                return null;
            }
        }

        return change;
    }));

With the getControlText() and getControlNewText() methods we were able to pick up the current and post-editing text (without the change having been effected yet). The above code cancels (Return null) any change that contains in its text a non-numeric element. I won’t go into details of how to use Textformatter because it is too wide, but you can see examples here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.