public void caretUpdate ( CaretEvent event ) { String text = getText ( ) . trim ( ) ; String emptyMask = maskPattern . replace ( ' ' , placeholder ) ; if ( text . length ( ) == 0 || text . equals ( emptyMask ) ) { setForeground ( Color . BLACK ) ; return ; } try { Date date = dateFormatter . parse ( getText ( ) ) ; if ( dateUtil . checkDate ( date ) ) { setForeground ( darkGreen ) ; } else { setForeground ( Color . RED ) ; } } catch ( Exception e ) { setForeground ( Color . RED ) ; } }
After any user input the value of the textfield is proofed . Depending on being a valid date the value is colored green or red .