首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaFX :侦听器的TextField最大值和最小值

JavaFX :侦听器的TextField最大值和最小值
EN

Stack Overflow用户
提问于 2017-01-06 20:07:55
回答 3查看 1.9K关注 0票数 1

我需要限制文本字段的文本属性的间隔。

代码语言:javascript
运行
复制
int maxLength = 64;
    int minLength = 0;
    txtSeuil.textProperty().addListener((v, oldValue, newValue) -> {
        if (!newValue.matches("\\d*")) {
            txtSeuil.setText(newValue.replaceAll("[^\\d*{1,2}]", ""));
            if (txtSeuil.getText().length() > maxLength || txtSeuil.getText().length() < minLength) {
                String s = txtSeuil.getText().substring(0, maxLength);
                txtSeuil.setText(s);
            }
        }

    });

该字段只接受数字,而不接受任何数字,而不仅仅是区间值。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-01-06 21:15:25

如果我理解正确,那么您将尝试实现一个仅允许区间0,64内的值的number字段。根据这个答案的说法,TextFormatter是实现这种功能的推荐方法。看看这个米维,它可以解决您的问题:

代码语言:javascript
运行
复制
public class RestrictedNumberFieldDemo extends Application {

    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage primaryStage) {
        TextField numField = new TextField();
        numField.setTextFormatter(new TextFormatter<Integer>(change -> {
            // Deletion should always be possible.
            if (change.isDeleted()) {
                return change;
            }

            // How would the text look like after the change?
            String txt = change.getControlNewText();

            // There shouldn't be leading zeros.
            if (txt.matches("0\\d+")) {
                return null;
            }

            // Try parsing and check if the result is in [0, 64].
            try {
                int n = Integer.parseInt(txt);
                return 0 <= n && n <= 64 ? change : null;
            } catch (NumberFormatException e) {
                return null;
            }
        }));

        primaryStage.setScene(new Scene(numField));
        primaryStage.show();
    }

}
票数 2
EN

Stack Overflow用户

发布于 2017-01-06 20:38:02

要解决您的问题,可以为TextFormatter实现自定义的TextFormatter。它只允许输入数字并限制字符串的长度。下面是展示它如何工作的片段:

代码语言:javascript
运行
复制
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.stage.Stage;

public class Main5 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        TextField textField = new TextField();
        textField.setTextFormatter(new TextFormatter<Integer>(change -> {
            if (!change.getText().isEmpty()) {
                return change.getText().matches("\\d+") && change.getControlNewText().length() <= 5 ? change : null;
            }

            return change;
        }));

        primaryStage.setScene(new Scene(textField));
        primaryStage.show();
    }
}
票数 2
EN

Stack Overflow用户

发布于 2017-01-06 21:02:21

你的问题是:

如果正则表达式匹配,则不执行长度检查。但是文本可以是一个数字序列,都是长的。

你们需要独立地做那些检查。

另外,在某些情况下,您正在设置一个新值,这再次触发相同的检查。当然,它们将导致将String计算为有效输入,但通过在存储的ChangeListener中引入字段来防止再次检查,监听器当前是否正在执行:

代码语言:javascript
运行
复制
txtSeuil.textProperty().addListener(new ChangeListener<String>() {

    private boolean validating = false;

    @Override
    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
        if (!validating) {
            validating = true;
            String newText = newValue;
            boolean modify = false;
            if (!newValue.matches("\\d*")) {
                newText = newValue.replaceAll("[^\\d*{1,2}]", "");
                modify = true;
            }
            if (newText.length() > maxLength || newText.length() < minLength) {
                newText = newText.substring(0, maxLength);
                modify = true;
            }
            if (modify) {
                txtSeuil.setText(newText);
            }
            validating = false;
        }
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41513553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档