How to limit the amount of characters a javafx textfield


I´m using a FXML to set my form, but I need to set the limit of characters in textfields. How can I made this ?

3/28/2014 1:39:27 PM

Accepted Answer

Here is my solution to limit the length of a textfield. I would not recommend solutions which use a listener (on text property or on length property), they do not behave correctly in all situations (for what I have seen). I create an HTML input text with a max length, and compare it to my textfield in JavaFX. I had the same behavior with paste operations (Ctrl + V), cancel operations (Ctrl + Z) in both cases. The goal here is to check if the text is valid BEFORE modifying the textfield. We could use a similar approach for a numeric text field.

import java.util.Objects;
import javafx.scene.control.TextField;   

public class LimitedTextField extends TextField {

    private final IntegerProperty maxLength;

    public LimitedTextField() {
        this.maxLength = new SimpleIntegerProperty(-1);

    public IntegerProperty maxLengthProperty() {
        return this.maxLength;

    public final Integer getMaxLength() {
        return this.maxLength.getValue();

    public final void setMaxLength(Integer maxLength) {
        Objects.requireNonNull(maxLength, "Max length cannot be null, -1 for no limit");

    public void replaceText(int start, int end, String insertedText) {
        if (this.getMaxLength() <= 0) {
            // Default behavior, in case of no max length
            super.replaceText(start, end, insertedText);
        else {
            // Get the text in the textfield, before the user enters something
            String currentText = this.getText() == null ? "" : this.getText();

            // Compute the text that should normally be in the textfield now
            String finalText = currentText.substring(0, start) + insertedText + currentText.substring(end);

            // If the max length is not excedeed
            int numberOfexceedingCharacters = finalText.length() - this.getMaxLength();
            if (numberOfexceedingCharacters <= 0) {
                // Normal behavior
                super.replaceText(start, end, insertedText);
            else {
                // Otherwise, cut the the text that was going to be inserted
                String cutInsertedText = insertedText.substring(
                        insertedText.length() - numberOfexceedingCharacters

                // And replace this text
                super.replaceText(start, end, cutInsertedText);

Tested with JavaFX 8 and Java 8u45

7/1/2015 12:36:41 PM

You can't directly set a limit to number of characters. But you can add a listener to lengthProperty() of the textfield

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TextFieldLimit extends Application {
    private static final int LIMIT = 10;

    public static void main(String[] args) {

    public void start(final Stage primaryStage) {

        final TextField textField = new TextField();
        textField.lengthProperty().addListener(new ChangeListener<Number>() {

            public void changed(ObservableValue<? extends Number> observable,
                    Number oldValue, Number newValue) {
                if (newValue.intValue() > oldValue.intValue()) {
                    // Check if the new character is greater than LIMIT
                    if (textField.getText().length() >= LIMIT) {

                        // if it's 11th character then just setText to previous
                        // one
                        textField.setText(textField.getText().substring(0, LIMIT));

        VBox vbox = new VBox(20);

        Scene scene = new Scene(vbox, 400, 300);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow