Maskformatter with setValidCharacters gets cleared when any JButton is clicked.
I am having a problem that I hope someone can help me with because I can't seem to find anything online about it. I am using a MaskFormatter to format a JFormattedTextField and for some reason if I restrict the field to just numbers then whenever any JButton is clicked the field gets cleared even if the JButton doesn't have an actionlistener. It doesn't do this if I allow any character but still use the MaskFormatter to restrict the number of characters. Below is my code.
MaskFormatter cellNumberFormat = null; //Create a variable to format cell phone numbers
try {
cellNumberFormat = new MaskFormatter("(***) ***-****");
} catch (ParseException ex) {
Logger.getLogger(UserContacts.class.getName()).log (Level.SEVERE, null, ex);
}
cellNumberFormat.setValidCharacters("1234567890"); //Limit what characters can be entered in the cell number field
cellNumberFie = new JFormattedTextField(cellNumberFormat); //Create a text field for cell numbers that limits how the number can be entered
So with the way it is written above whenever I click any JButton, even with no actionlistener added to it, then the field gets cleared. But if I comment out the line with the setValidCharacters it doesn't do that. Anyone seen this before?
Re: Maskformatter with setValidCharacters gets cleared when any JButton is clicked.
Is there any reason you are not just using the # character instead of *?
So you would say: cellNumberFormat = new MaskFormatter("(###) ###-####");
and then you can get rid of the setValidCharacters() command altogether.
Here is the API for MaskFormatter: MaskFormatter (Java Platform SE 6)