2015-04-21 26 views
1

我有一个JTexTField,我希望用户输入一个人的名字。我认为该名称应该包含[a-zA-Z],.space示例Mr. Bill。我正在使用DocumentFilter来验证用户输入。但是,我无法弄清楚我应该如何在我的DocumentFilter中设置它。使用DocumentFilter过滤字符串,空格和点(。)的JTextField

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

任何关于如何验证一个人姓名的建议都被接受。

这里是我的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(); 

     } 
     }); 
    } 
} 
+1

加一,但我缺少引号/标点符号,特别是单个 – mKorbel

+0

@mKorbel是的,我在想单个''',但不能决定它 - 例如'O'Rourke'。谢谢你提到它。 – JWizard

回答

2

你可以使用一个JFormattedTextFieldMaskFormatterMaskFormatter允许您指定有效字符的String

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

在幕后,格式化的文本字段使用DocumentFilter。有关更多信息和示例,请参阅How to Use Formatted Text Fields上的Swing教程部分。

您也可以尝试在论坛/网页上搜索正则表达式DocumentFilter。这种类型的过滤器通常是可重用的,因为您只需指定正则表达式。例如:Trouble using regex in DocumentFilter for JTextField

+0

除了使用蜂鸣声之外,我能否通知用户输入了无效的输入?我想通过消息对话框通知用户。 – JWizard

+0

那么,假设我已经诉诸你的实现,我该如何加入我的'正则表达式'来验证'[a-zA-Z]'和'\ S'-白色空间?当我把它放在'setValidCharacters(“....”)'中时,它看起来如何? -谢谢。 – JWizard

+0

我提出了两种不同的解决方案。一个是在你的DocumentFilter中使用正则表达式。我对编写一个有效的正则表达式一无所知,因此我无法理解这些细节。第二个是使用JFormattedTextField。在这种情况下,创建一个简单的包含JFormattedText字段的JFrame,然后测试我给出的3行代码,以了解它是如何工作的。 – camickr

0

我发现的解决方案可能需要进行修改以解决上述所有验证问题。我已经使用DocumentFilter来消除\p{Punct} - 除了.'从这个集合和[0 -9]

这里是我使用的代码:

public class NameValidator extends DocumentFilter{ 
@Override 
public void insertString(FilterBypass fb, int off 
        , String str, AttributeSet attr) 
          throws BadLocationException 
{ 
    // remove 0-9 !"#$%&()*+,-/:;<=>[email protected][\]^_`{|}~ 
    //back space character is skipped here! 
    fb.insertString(off, str.replaceAll("^[0-9\\_\\(\\)@!\"#%&*+,\\-:;<>=?\\[\\]\\^\\~\\{\\}\\|\\/]", ""), attr); 
} 
@Override 
public void replace(FilterBypass fb, int off 
     , int len, String str, AttributeSet attr) 
         throws BadLocationException 
{ 
    // remove 0-9 !"#$%&()*+,-/:;<=>[email protected][\]^_`{|}~ 
    fb.replace(off, len, str.replaceAll("^[0-9\\_\\(\\)@!\"#%&*+,\\-:;<>=?\\[\\]\\^\\~\\{\\}\\|\\/]", ""), attr); 
     } 

    } 

任何修改被接受,以满足规定的验证在原来的问题。