I created a formattedfield which takes only integer as input and i set its border to be a red line border if any alphabet is typed. Now i want to set default border if user corrects the error. how can i set default border.
Thanks For Your Help
This is my code :
if(!(Character.isDigit(evt.getKeyChar()))){
evt.consume();
Toolkit.getDefaultToolkit().beep();
flatNoField_addUser.setBorder(BorderFactory.createLineBorder(Color.red));
}
else{
?????
}
First of all, it would look like you are using a KeyListener
to try and filter a text field, this is a bad idea and can lead to many issues and inconsistencies. Instead you should be using a DocumentFilter
, which is designed to do just this.
You could store a reference to the original border before your start...
Border border = flatNoField_addUser.getBorder();
And simply reapply it when you need to.
A more generic solution would be to ask the UIManger...
Border border = UIManager.getBorder("TextField.border");
The problem with this is it does not take into consideration the use case where the field had a non-default border