Search code examples
javauser-interfacevalidationjtextfield

Input-validation based on entire content of JTextField, not just the last typed character


Is there a way to validate text in a JTextField while you type, based on what you already typed in that field? Should I create a keyEventListener of some sort, or is there a way to override the insertString method to let it do that. I prefer the latter, but it only gives you control over the last character that was typed, not the entire string of text already present in the text field. I want it to beep as soon as one tries to enter more than one decimal point, and not adding that second decimal point to the text field.

Thanks for your help, Erik


Solution

  • What's the problem with adding a listener? That's what they are there for.

    You might also want to have a look at JFormattedTextField.