2011-12-08 61 views
0

在NetBeans中,如何爲JFormattedTextField設置無效字符,以便它不允許:|[]等等。如何禁止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);

希望這將有助於別人有一天。

回答

1

最舒適的方式是通過炭添加DocumentListenerPattern,瓦魯爾足球俱樂部的JFormattedTextField可以inseted(字符塊)或添加字符,我建議來覆蓋字符的插入塊兩種方法

insertUpdate() 

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

會更好的實現Pattern p = Pattern.compile("decision char filtering")直接