我正在追踪,http://www.java2s.com/Tutorial/Java/0240__Swing/RegexFormatterwithaJFormattedTextField.htm。在给定的示例中,如何在不遵循格式化程序的RegEx格式的情况下更改其输入文本上的JFormattedTextField的前景色/文本颜色?
最佳答案
当用户尝试使用JFormattedTextField
更改焦点时,可以更改InputVerifier
的前景色。从此完整的example开始,在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;
}
}
要在键入时查看更改,请使用
DocumentListener
以类似的方式评估DateFormat
实例。关于java - 如何在不遵循RegEx格式的情况下更改JFormattedTextField输入文本的前景色/文本颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37671340/