将 TextField 限制在特定范围内的数字 JavaFX?

Limit TextField to a specific range of number JavaFX?

您好,我需要限制 TextField javaFX 的输入,不仅是整数,还有 1 - 19 之间的数字 only.For 例如,我应该被允许输入:“3”,“19”...但不是: "33" , 44 .. 例如:What is the recommended way to make a numeric TextField in JavaFX? 但这限制了文本字段仅用于整数。

我现在修改了我的代码。此代码允许您输入“13”以及仅输入 13。它还会检查输入是否在范围内。

演示应用程序

@Override
    public void start(Stage primaryStage) throws Exception {
        TextField textField = new TextField();
        GridPane gridPane = new GridPane();
        Scene scene = new Scene(gridPane);
        gridPane.add(textField, 0, 0);

        final int MIN = 1;
        final int MAX = 19;

        UnaryOperator<TextFormatter.Change> filter = change -> {

            //if something got added
            if (change.isAdded()) {

                //if change is " add "" in texfield
                if (change.getText().equals("\"")) {

                    if (!change.getControlText().contains("\"")) {
                        change.setText("\"\"");
                        return change;
                    } else {
                        //if textfield already contains ""
                        return null;
                    }

                } else {
                    //If Input is not a number don't change anything
                    if (change.getText().matches("[^0-9]")) {
                        return null;
                    }

                    //If change don't contains " check if change is in range
                    if (!change.getControlText().contains("\"")) {
                        if (Integer.parseInt(change.getControlNewText()) < MIN || Integer.parseInt(change.getControlNewText()) > MAX) {
                            return null;
                        }
                    } else {
                        //if change contains "" remove "" and check if is in range
                        String s = change.getControlNewText();

                        s = s.replaceAll("[\"]", "");
                        int value = Integer.parseInt(s);

                        if (value < MIN || value > MAX) {
                            return null;
                        }
                    }
                }
            }

            return change;
        };

        textField.setTextFormatter(new TextFormatter<>(filter));

        primaryStage.setScene(scene);
        primaryStage.show();
    }

您可以使用 regex 允许您的特定数字范围 (1-19) 并在 TextField 的 TextFormatterfilter.

上添加该验证

正则表达式 => ([1-9]|1[0-9])

  • [1-9] 任一 TextField 都允许您输入 1 到 9 个数字
  • 1[0-9] 或 TextField 允许您输入 10 到 19 个数字

正则表达式电路

TextField 验证演示

public class TextFieldValidationDemo extends Application {

    private static final String REGEX_VALID_INTEGER = "([1-9]|1[0-9])";

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane root = new BorderPane();
        root.setCenter(getRootPane());
        primaryStage.setScene(new Scene(root, 200, 200));
        primaryStage.show();
    }

    private BorderPane getRootPane() {
        BorderPane root = new BorderPane();
        root.setCenter(getTextField());
        return root;
    }

    private TextField getTextField() {
        TextField field = new TextField();
        field.setTextFormatter(new TextFormatter<>(this::filter));
        return field;
    }

    private TextFormatter.Change filter(TextFormatter.Change change) {
        if (!change.getControlNewText().matches(REGEX_VALID_INTEGER)) {
            change.setText("");
        }
        return change;
    }
}