【问题标题】:How to disallow characters in JFormattedTextField?如何禁止 JFormattedTextField 中的字符?
【发布时间】:2011-12-09 02:08:37
【问题描述】:

在 NetBeans 中,如何为 JFormattedTextField 设置无效字符,使其不允许:|[] 等?

没关系,我找到了解决方案:

MaskFormatter formatter = null;

        try {
            //# - Any valid number, uses Character.isDigit.
            //' - Escape character, used to escape any of the special formatting characters.
            //U - Any character (Character.isLetter). All lowercase letters are mapped to upper case.
            //L - Any character (Character.isLetter). All upper case letters are mapped to lower case.
            //A - Any character or number (Character.isLetter or Character.isDigit)
            //? - Any character (Character.isLetter).
            //* - Anything.
            //H - Any hex character (0-9, a-f or A-F).
            formatter = new MaskFormatter("******************************");
            formatter.setInvalidCharacters("|[]");
        } catch (java.text.ParseException ex) {
        }

然后,在 GUI 上右键单击 JFormattedTextField,单击自定义代码,对于 myFormattedTextField = new javax.swing.JFormattedTextField(); 旁边的下拉框,选择自定义创建并将格式化程序添加到 JFormattedTextField,如下所示:myFormattedTextField = new javax.swing.JFormattedTextField(formatter);

希望有一天这对其他人有所帮助。

【问题讨论】:

    标签: java swing netbeans jformattedtextfield


    【解决方案1】:

    最舒适的方法是添加DocumentListenerPattern,对JFormattedTextField 进行赋值可以插入(字符块)或逐个字符添加,我建议覆盖这两种方法

    insertUpdate()
    
    changedUpdate()//Plain text components don't fire these events.
    

    对于插入的字符块最好直接实现Pattern p = Pattern.compile("decision char filtering")

    【讨论】:

      猜你喜欢
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 2015-01-18
      • 2020-04-27
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多