2013-02-11 40 views
0

我在Sybase填充有信息從表中一個HashMap,利用這種結構IDocumentFilter使用一個HashMap作爲字典菜單,以驗證在一個JTextField

-index - CHARACTERS

---- 1 ---- 1234567890

---- 2 ----- ABCDEFG ..

我tryng設置新的CustomJTextField從JTextField中延伸,所以這種控制將有一個屬性附加傷害名爲MASK和我可以在此屬性中設置掩碼號碼,如下所示:

customtextField = new CustomTextField(20); 
customtextField.set_MASK(1); 

我已經有customtextField一些額外的屬性工作, 的面具屬性附加傷害的行爲不會讓未包含在表中的用戶寫的信,所以如果customtextField設置爲MASK(1 ),用戶只能夠只寫數字

,我需要使用的DocumentFilter,或任何建議幫助,我需要從數據庫(用戶要求)表中獲取的字典,

編輯*

由推薦im試圖得到一個DocumentFilter的例子,它只讓數組中包含的使用類型字符(由HAshMap創建)

回答

2

您可能希望改爲使用Document Filter

這將允許您構建過濾器來限制用戶實際可以鍵入的內容,而不是依靠後驗證。

檢查here的一些例子

更新

這是出奇的簡單。使用我鏈接的例子。我相信你能夠適應你的需求。

enter image description here

public class TestDocumentFilter01 { 

    public static void main(String[] args) { 
     new TestDocumentFilter01(); 
    } 

    public TestDocumentFilter01() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 
      add(createField("1234567890"), gbc); 
      add(createField("stackoverflow"), gbc); 
      add(createField("abcdefghijklmnopqrstuvwxyz "), gbc); 
     } 

     protected JTextField createField(String mask) { 
      JTextField field = new JTextField(10); 
      MaskFilter df = new MaskFilter(); 
      df.setMask(mask); 
      ((AbstractDocument) (field.getDocument())).setDocumentFilter(df); 
      return field; 
     } 

    } 

    public class MaskFilter extends DocumentFilter { 

     private char[] maskSet; 
     private String mask; 

     public void setMask(String mask) { 
      this.mask = mask; 
      this.maskSet = mask.toCharArray(); 
      Arrays.sort(this.maskSet); 
     } 

     public String getMask() { 
      return mask; 
     } 

     public void insertString(DocumentFilter.FilterBypass fb, int offset, 
                 String string, AttributeSet attr) 
         throws BadLocationException { 
      StringBuffer buffer = new StringBuffer(string); 
      for (int i = buffer.length() - 1; i >= 0; i--) { 
       char ch = buffer.charAt(i); 
       if (Arrays.binarySearch(maskSet, ch) < 0) { 
        buffer.deleteCharAt(i); 
       } 
      } 
      super.insertString(fb, offset, buffer.toString(), attr); 
     } 

     public void replace(DocumentFilter.FilterBypass fb, 
               int offset, int length, String string, AttributeSet attr) throws BadLocationException { 
      if (length > 0) { 
       fb.remove(offset, length); 
      } 
      insertString(fb, offset, string, attr); 
     } 
    } 
} 
+0

你必須從存儲在一個HashMap角色創建的過濾器的螞蟻的例子嗎? ,表中包含允許的字符, – user2030181 2013-02-11 04:11:11

+0

@ user2030181終於找到了一段時間:P – MadProgrammer 2013-02-12 09:34:07

相關問題