I have 4 JTextFields that should only accept certain characters:
- binary digits (
0,1) - octal digits, so (
0-7) - all digits (
0-9) - all hexadecimal characters (
0-9,a-f,A-F)
The user must not be able to input a forbidden character.
I know how I could validate the input afterwards, but not how to filter it.
I tried using a MaskFormatter, but then I can't enter anything at all.
MaskFormatter binaryFormatter = new MaskFormatter();
binaryFormatter.setValidCharacters("01");
JFormattedTextField binaryText = new JFormattedTextField(binaryFormatter);
You don't want to format the value, you want to filter the content. Use a
DocumentFilteron a plain onJTextFieldStart by having a look at Implementing a DocumntFilter and Examples for more details...
As an example, a "binary filter", which will only accept
0and1Which can be applied directly to the field's
Document: