控制键盘输入到javafx TextField [英] Control keyboard input into javafx TextField

查看:2784
本文介绍了控制键盘输入到javafx TextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想控制Javafx TextField的输入,以便我只允许输入数字,这样如果超出最大字符数,则不会对文本框进行任何更改。

I want to control the input into a Javafx TextField so that I can allow only Numeric input, and so that if the max characters are exceeded, then no change will be made to the textbox.

编辑:根据评论中的建议,我使用了JavaFX项目负责人建议的方法。它可以很好地阻止输入字母。我只是需要它来过滤特殊字符。我尝试将过滤器更改为(text.matchs([0-9]),但不允许输入退格。

edit: Based on a recommendation in the comments, I used the method suggested by the JavaFX project lead. It works great to stop letters from being entered. I just need it to also filter special characters. I tried changing the filter to (text.matchs("[0-9]") but that did not allow backspace to be entered.

edit2:找出过滤器关于特殊字符和长度。这是我的最终代码。感谢输入fellas。

edit2: Figured out a filter on special chars and length. Here is my final code. Thanks for the input fellas.

这是我创建的TextField类:

Here is the TextField class i have created:

import javafx.scene.control.TextField;

public class AttributeTextField extends TextField{

    public AttributeTextField() {
        setMinWidth(25);
        setMaxWidth(25);
    }

    public void replaceText(int start, int end, String text) {
        String oldValue = getText();
        if (!text.matches("[a-z]") && !text.matches("[\\\\!\"#$%&()*+,./:;<=>?@\\[\\]^_{|}~]+")) {
            super.replaceText(start, end, text);
        }
        if (getText().length() > 2 ) {
            setText(oldValue);
        }
    }

    public void replaceSelection(String text) {
        String oldValue = getText();
        if (!text.matches("[a-z]") && !text.matches("[\\\\!\"#$%&()*+,./:;<=>?@\\[\\]^_{|}~]+")) {
            super.replaceSelection(text);
        }
        if (getText().length() > 2 ) {
            setText(oldValue);
        }
    }
}

注意:我已阅读在JavaFX中制作数字TextField的推荐方法是什么?这篇文章,这个解决方案对我不起作用。它只会被解雇在输入数字之后。意思是某人可以在框中键入字母文本,并且它会允许它直到他们将焦点移离文本字段。此外,他们可以输入大于允许的数字,但验证不会发生在每个按键上,而是相反,焦点转移('改变'事件)。

Note: I have read What is the recommended way to make a numeric TextField in JavaFX? this post, and this solution does not work for me. It only gets fired after the number has been entered. Meaning someone could type alphabetic text into the box, and it would allow it until they moved focus away from the textfield. Also, they can enter numbers larger than allowed, but validation happens not on each keypress, but instead after focus shift ('changed' event).

推荐答案

最终解决方案。禁止使用字母和特殊字符并强制执行字符限制。

Final solution. Disallows alphabetic and special characters and enforces character limit.

import javafx.scene.control.TextField;

public class AttributeTextField extends TextField{

    public AttributeTextField() {
        setMinWidth(25);
        setMaxWidth(25);
    }

    public void replaceText(int start, int end, String text) {
        String oldValue = getText();
        if (!text.matches("[A-Za-z]") && !text.matches("[\\\\!\"#$%&()*+,./:;<=>?@\\[\\]^_{|}~]+")) {
            super.replaceText(start, end, text);
        }
        if (getText().length() > 2 ) {
            setText(oldValue);
        }
    }

    public void replaceSelection(String text) {
        String oldValue = getText();
        if (!text.matches("[A-Za-z]") && !text.matches("[\\\\!\"#$%&()*+,./:;<=>?@\\[\\]^_{|}~]+")) {
            super.replaceSelection(text);
        }
        if (getText().length() > 2 ) {
            setText(oldValue);
        }
    }
}

这篇关于控制键盘输入到javafx TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆