Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX TextArea onChange event

Good day developers :)

Does JavaFX component, TextArea, have support for some event like onTextChange or similar? Yes, I know for keyPressed, keyTyped ... but how to handle event if another "action" do changes on TextArea (eg. txArea.setText("some text")).

like image 924
Đorđe Zeljić Avatar asked Mar 25 '12 18:03

Đorđe Zeljić


People also ask

Can I use onchange in textarea?

The onchange element could be used on a textarea to launch a script after the user finishes filling in the field. The script could do something like enable the form submit button or enable a follow-up field that shouldn't be completed until the textarea has received input.

What is addListener in JavaFX?

addListener. void addListener(ChangeListener<? super T> listener) Adds a ChangeListener which will be notified whenever the value of the ObservableValue changes. If the same listener is added more than once, then it will be notified more than once.


3 Answers

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

    textArea.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(final ObservableValue<? extends String> observable, final String oldValue, final String newValue) {
            // this will run whenever text is changed
        }
    });
like image 181
ceklock Avatar answered Sep 18 '22 14:09

ceklock


As with all of JavaFX, just add a listener to the TextArea textProperty().

like image 31
Jonathan Giles Avatar answered Sep 16 '22 14:09

Jonathan Giles


Using Lambda expressions

textArea.textProperty().addListener((obs,old,niu)->{
    // TODO here
});
like image 39
Wesos de Queso Avatar answered Sep 19 '22 14:09

Wesos de Queso