I am following, http://www.java2s.com/Tutorial/Java/0240__Swing/RegexFormatterwithaJFormattedTextField.htm. In the given example, How to change foreground/text color of JFormattedTextField on its input text not obeying the RegEx format of the formatter?
Asked
Active
Viewed 192 times
1 Answers
3
You can change the foreground color of the JFormattedTextField
when the user attempts to change focus using an InputVerifier
. Starting from this complete example, condition the color in your implementation of shouldYieldFocus()
.
@Override
public boolean shouldYieldFocus(JComponent input) {
if (verify(input)) {
tf.setValue(date);
tf.setForeground(Color.black);
return true;
} else {
tf.setForeground(Color.red);
return false;
}
}
To see changes while typing, use a DocumentListener
that evaluates DateFormat
instances in a similar way.