I have a JFormattedTextField
which accepts number of 8 digits only, but when I try to clear the textfield with backspace button it doesnt delete first character of number (same behavior with delete button too), I have to presee Esc key to delete this character each time.
NumberFormat intFormat = NumberFormat.getIntegerInstance();
intFormat.setGroupingUsed(false);
NumberFormatter numberFormatter = new NumberFormatter(intFormat);
numberFormatter.setValueClass(Integer.class);
numberFormatter.setAllowsInvalid(false);
numberFormatter.setMinimum(0);
numberFormatter.setMaximum(99999999);
releaseNoTextField = new JFormattedTextField(numberFormatter);
what's the problem here ?
releaseNoTextField.setText("")
is not working, is there another way to do this ?I have a JFormattedTextField which accepts number of 8 digits only, but when I try to clear the textfield with backspace button it doesnt delete first character of number (same behavior with delete button too), I have to presee Esc key to delete this character each time.
This is a restriction been applied by numberFormatter.setAllowsInvalid(false);
which sees a blank String
(""
) as not been a valid numerical value. If you use numberFormatter.setAllowsInvalid(true);
you can remove all the characters, but you can also enter any value you like.
The field will be validated AFTER the user leaves the field though.
If you don't care about the value been formatted (12, 345, 678
), then you could use a DocumentFilter
applied to a plain JTextField
an implement the logic you require within it. See Implementing a Document Filter and DocumentFilter Examples for more details.
Also clearing this textfield with releaseNoTextField.setText("") is not working, is there another way to do this ?
You should (always) be using setValue
for JFormattedTextField
and you should true setValue(null)
to clear the field