简体   繁体   English

Java JTextField长度和过滤器

[英]Java JTextField Length and filter

I am trying to create Java IMEI checksum calculator. 我正在尝试创建Java IMEI校验和计算器。 The checksum is the last digit in the IMEI 15 digits. 校验和是IMEI 15位数字中的最后一位数字。

Therefore, I need to create JTextField which limit to 14 characters and also restricted to digits only! 因此,我需要创建JTextField,它限制为14个字符,并且也仅限于数字!

For the 14 characters limitation I've made a class extends PlainDocument as follow: 对于14个字符的限制,我使一个类扩展了PlainDocument,如下所示:

public final class LengthRestrictedDocument extends PlainDocument {

  private final int limit;

  public LengthRestrictedDocument(int limit) {
    this.limit = limit;
  }

  public void insertString(int offs, String str, AttributeSet a)
      throws BadLocationException {
    if (str == null)
      return;

    if ((getLength() + str.length()) <= limit) {
      super.insertString(offs, str, a);
    }
  }
}

Then I applied it to the JTextField like this: 然后我将其像这样应用于JTextField:

    PlainDocument document = new LengthRestrictedDocument(14);
    tfImei.setDocument(document); //Restricting inputs to 14 digits

For the digits only I've tried to use DocumentFilter like this: 对于数字,我仅尝试使用DocumentFilter,如下所示:

DocumentFilter docFilter = new DocumentFilter() {
        Pattern regEx = Pattern.compile("\\d+");

        @Override
        public void replace(FilterBypass fb, int offset, int length, String   text, AttributeSet attrs) throws BadLocationException {
            Matcher matcher = regEx.matcher(text);
            if (!matcher.matches()) {
                return;
            }
            super.replace(fb, offset, length, text, attrs);
        }
    };

I am using it as follow: 我使用它如下:

    PlainDocument document = new LengthRestrictedDocument(14);
    document.setDocumentFilter(docFilter);
    tfImei.setDocument(document); //Restricting inputs to 14 digits

it do the job for the digits only but it cancel the 14 digits limitation. 它只执行数字的工作,但取消了14位的限制。

Can someone help to impliment such functionality? 有人可以帮助实现这种功能吗? Restrict to 14 charachters and digits only? 仅限14个字符和数字吗?

For the 14 characters limitation I've made a class extends PlainDocument as follow: 对于14个字符的限制,我使一个类扩展了PlainDocument,如下所示:

This is an older approach. 这是一种较旧的方法。 The newer approach is to use a DocumentFilter. 更新的方法是使用DocumentFilter。 Check out the section from the Swing tutorial on Implementing a DocumentFilter for a working example. 请查看Swing教程中有关实现DocumentFilter的部分 ,以获取工作示例。

For the digits only I've tried to use DocumentFilter like this. 对于数字,我仅尝试使用DocumentFilter这样。 it do the job for the digits only but it cancel the 14 digits limitation. 它只执行数字的工作,但取消了14位的限制。

Don't use a custom Document and a DocumentFilter. 不要使用自定义文档和DocumentFilter。

Instead you can combine the functionality into a single DocumentFilter. 相反,您可以将功能组合到单个DocumentFilter中。

Or for an even simpler solution you can use a JFormattedTextField . 或者,对于更简单的解决方案,可以使用JFormattedTextField You can specify a mask that controls the number of digits and the type of each digit: 您可以指定一个掩码来控制数字位数和每个数字的类型:

MaskFormatter format = new MaskFormatter( "##############" );
JFormattedTextField ftf = new JFormattedTextField( mf );

The tutorial has a section on How to Use Text Fields for more information and examples. 本教程包含有关How to Use Text Fields的部分, How to Use Text Fields获取更多信息和示例。

For a more flexible approach you may want to look at Chaining Document Filters . 对于更灵活的方法,您可能需要查看链接文档过滤器 It allows you to combine multiple DocumentFilters into one filter so if any edit fails the insertion is prevented. 它允许您将多个DocumentFilters组合到一个过滤器中,因此,如果任何编辑失败,则可以防止插入。 This allows you to create reusable filters that you can use in different situations. 这使您可以创建可在不同情况下使用的可重复使用的过滤器。

Finally I've got this to work! 终于,我有了这个工作! Yes, I've used Document Filter as following: 是的,我使用了文档过滤器,如下所示:

        DocumentFilter docFilter = new DocumentFilter() {
        Pattern regEx = Pattern.compile("\\d+");
        final int maxCharLength = 14;

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            Matcher matcher = regEx.matcher(text);
            if ((fb.getDocument().getLength() + text.length()) <= maxCharLength && matcher.matches()) {
                super.replace(fb, offset, length, text, attrs);
            }else{
                Toolkit.getDefaultToolkit().beep();
            }
        }
    };


    AbstractDocument absDoc = (AbstractDocument)tfImei.getDocument();
    absDoc.setDocumentFilter(docFilter);

This will filter to digits only and with length of 14! 这将仅过滤长度为14的数字!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM