简体   繁体   中英

How to change foreground/text color of JFormattedTextField on its input text not obeying the RegEx format?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM