Search code examples
javajavafxfxmlscenebuilder

Java FXML - Display String length of text input field


Folks, I want to display the string length of the input text field below the input text field. Refer the screenshot below.

Basically, I want the count to be displayed in a dynamic fashion i.e whenever a user changes the input, the count should also change accordingly.

How can this be achievable using javafx?

Also, how can the string length value be injected in the FXML file?

enter image description here


Solution

  • Bindings.length returns a IntegerBinding containing the length of the sting value in a StringProperty. asString allows you to format the value as string:

    label.textProperty().bind(Bindings.length(textField.textProperty())
                                      .asString("String length: %d"));