Reputation: 1446
In NetBeans, how can I setup invalid characters for JFormattedTextField
so that it does not allow: |
, [
, ]
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();
select custom creation and add formatter to JFormattedTextField as following: myFormattedTextField = new javax.swing.JFormattedTextField(formatter);
Hope this will help somebody else one day.
Upvotes: 0
Views: 1189
Reputation: 109815
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
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
Upvotes: 1