[英]How to disallow characters in JFormattedTextField?
In NetBeans, how can I setup invalid characters for JFormattedTextField
so that it does not allow: |
在NetBeans中,我怎么能进行设置无效字符JFormattedTextField
,以便它不允许: |
, [
, ]
and etc.? , [
, ]
等?
Never mind, I figured out the solution: 没关系,我想出了解决方案:
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) {
}
then, on the GUI right click on the JFormattedTextField, click on Customize Code, for the dropdown box beside myFormattedTextField = new javax.swing.JFormattedTextField();
然后,在GUI上右键单击JFormattedTextField,单击“自定义代码”,对于myFormattedTextField = new javax.swing.JFormattedTextField();
旁边的下拉框myFormattedTextField = new javax.swing.JFormattedTextField();
select custom creation and add formatter to JFormattedTextField as following: myFormattedTextField = new javax.swing.JFormattedTextField(formatter);
选择自定义创建并将格式化程序添加到JFormattedTextField,如下所示: myFormattedTextField = new javax.swing.JFormattedTextField(formatter);
Hope this will help somebody else one day. 希望有一天能对别人有所帮助。
most confortable way is add DocumentListener and there Pattern , valur to the JFormattedTextField
could inseted (block of chars) or added char by char, I suggest to override both methods 最舒适的方法是添加DocumentListener ,然后可以将Pattern ,valur插入JFormattedTextField
或按字符添加char,我建议重写这两种方法
insertUpdate()
changedUpdate()//Plain text components don't fire these events.
for inserted block of chars would be better implements Pattern p = Pattern.compile("decision char filtering")
directly 对于插入的字符块,最好直接实现Pattern p = Pattern.compile("decision char filtering")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.