Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the amount of characters a javafx textfield

Tags:

javafx

fxml

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 ?

like image 966
Bran Stark Avatar asked Mar 28 '14 13:03

Bran Stark


People also ask

How do you make a TextField Uneditable JavaFX?

In FXML, add editable="false" to your TextField tag. Or uncheck the "Editable" checkbox in Scene Builder.

Can you display multiple lines of text in a label JavaFX?

You can display multi-line read-only text in a Label . If the text has \n (newline) characters in it, then the label will wrap to a new line wherever the newline character is.

Can you disable editing of a TextField JavaFX?

you call setVisible(boolean) in order to make it visible or to hide it. You can call setEditable(boolean) to make the field editable or not and finally the setDisable(boolean) to make the field unable to be clicked etc.


2 Answers

One more elegance solution

Pattern pattern = Pattern.compile(".{0,25}");
    TextFormatter formatter = new TextFormatter((UnaryOperator<TextFormatter.Change>) change -> {
        return pattern.matcher(change.getControlNewText()).matches() ? change : null;
    });

    textField.setTextFormatter(formatter);

where 0 and 25 - min and max amount of chars. + ability to set a pattern of input text

like image 170
Hellomr Avatar answered Oct 26 '22 15:10

Hellomr


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) {
        launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {

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

            @Override
            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);
        vbox.getChildren().add(textField);

        Scene scene = new Scene(vbox, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
like image 33
ItachiUchiha Avatar answered Oct 26 '22 16:10

ItachiUchiha