使用DocumentFilter过滤JTextField的字符串,空格和点(.) [英] Filtering a JTextField for strings, space and a dot (.) using a DocumentFilter

查看:172
本文介绍了使用DocumentFilter过滤JTextField的字符串,空格和点(.)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTexTField,我希望用户输入一个人的名字.我已经知道该名称应包含[a-zA-Z].space示例 Mr. Bill .我正在使用DocumentFilter来验证用户输入.但是,我不知道如何在DocumentFilter中进行设置.

I have a JTexTField that I would want a user to enter the name of a person. I have figured that the name should contain [a-zA-Z],. and space example Mr. Bill. I am using a DocumentFilter to validate user input.However, I cannot figure out how should I set this within my DocumentFilter.

问题:我应该如何修改过滤器以实现上述行为?

Question: How should I modify my filter to achieve the above behavior?

关于如何验证人名的任何建议都被接受.

这是我的DocumentFilter:

Here is my DocumentFilter:

public class NameValidator extends DocumentFilter{
@Override
public void insertString(DocumentFilter.FilterBypass fp, int offset,
        String string, AttributeSet aset) throws BadLocationException {
    int len = string.length();
    boolean isValidInteger = true;

    for (int i = 0; i < len; i++) {
        if (!Character.isLetter(string.charAt(i))) {
            isValidInteger = false;
            break;
        }
    }
    if (isValidInteger)
        super.insertString(fp, offset, string, aset);
    else {
        JOptionPane.showMessageDialog(null,
                "Please Valid Letters only.", "Invalid Input : ",
                JOptionPane.ERROR_MESSAGE);
        Toolkit.getDefaultToolkit().beep();
    }
}

@Override
public void replace(DocumentFilter.FilterBypass fp, int offset, int length,
        String string, AttributeSet aset) throws BadLocationException {
    int len = string.length();
    boolean isValidInteger = true;

    for (int i = 0; i < len; i++) {
        if (!Character.isLetter(string.charAt(i)) ) {
            isValidInteger = false;
            break;
        }
    }
    if (isValidInteger)
        super.replace(fp, offset, length, string, aset);
    else {
        JOptionPane.showMessageDialog(null,
                "Please Valid Letters only.", "Invalid Input : ",
                JOptionPane.ERROR_MESSAGE);
        Toolkit.getDefaultToolkit().beep();
     }
   }
 }

这是我的考试班:

public class NameTest {

private JFrame frame;

public NameTest() {
    frame = new JFrame();
    initGui();
}

private void initGui() {

    frame.setSize(100, 100);
    frame.setVisible(true);
    frame.setLayout(new GridLayout(2, 1, 5, 5));
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField name = new JTextField(15);
    ((AbstractDocument) name.getDocument())
            .setDocumentFilter(new NameValidator());
    frame.add(name);

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            NameTest nt = new NameTest();

         }
      });
    }
 }

推荐答案

您可以将JFormattedTextFieldMaskFormatter结合使用. MaskFormatter允许您指定有效字符的String.

You could use a JFormattedTextField with a MaskFormatter. The MaskFormatter allows you to specify a String of valid characters.

MaskFormatter mf = new MaskFormatter( "***************" );
mf.setValidCharacters(" .abcABC");
JFormattedTextField ftf = new JFormattedTextField( mf );

在后台,格式化的文本字段使用DocumentFilter.阅读Swing教程中如何使用带格式的文本字段有关更多信息和示例.

Behind the scenes the formatted text field uses a DocumentFilter. Read the section from the Swing tutorial on How to Use Formatted Text Fields for more information and examples.

您也可以尝试在论坛/网站上搜索正则表达式DocumentFilter.这种类型的过滤器通常是可重用的,因为您只需要指定regex表达式即可.例如:在JTextField的DocumentFilter中使用正则表达式遇到问题

You can also try searching the forum/web for a regex DocumentFilter. This type of filter is generally reusable because you just need to specify the regex expression. For example: Trouble using regex in DocumentFilter for JTextField

这篇关于使用DocumentFilter过滤JTextField的字符串,空格和点(.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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