【问题标题】:Limit TextField to a specific range of number JavaFX?将 TextField 限制为 JavaFX 的特定数字范围?
【发布时间】:2020-04-03 05:59:35
【问题描述】:

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

【问题讨论】:

  • 在调用 getText 时尝试 if 语句。比如:if(textField.getText 19) return;
  • @GipsyKing 我已经这样做了。但这不是我需要的。当我输入数字时,我需要这样做:stackoverflow.com/questions/7555564/… 但这将文本字段限制为整数。
  • 欢迎来到 SO。能否请您阅读how do I ask a good question,然后编辑问题以帮助我们更好地了解问题所在?

标签: javafx input filter textfield


【解决方案1】:

我现在已经修改了我的代码。此代码允许您输入“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();
    }

【讨论】:

    【解决方案2】:

    您可以使用 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-03
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      相关资源
      最近更新 更多