I have made a validator that pops up a message when the input is not correct but I want it to also change the jtextField color to red. Is there an easy way to do this?
Printable View
I have made a validator that pops up a message when the input is not correct but I want it to also change the jtextField color to red. Is there an easy way to do this?
Set the foreground color of the JTextField by calling setForeground with the color you wish to use (see the API for JTextField for more details on this method)
I tried that but Im not sure where to do it at in the validator or where here is what I tried but it tells me incompatible type:
Code:class var20Verifier extends InputVerifier {
public boolean verify(JComponent input) {
JTextField textField = (JTextField) input;
System.out.println("checking name");
String content1 = textField.getText();
if (content1.length()> 20){
JOptionPane.showMessageDialog(null, "Input is too long ");
[COLOR="Red"]return textField.setForeground(Color.red);[/COLOR] }
else if (content1.length()<= 0){
JOptionPane.showMessageDialog(null, "Input is too short ");
return !textField.getText().isEmpty();
}
else{
//return content.matches("\\d{3}");
return !textField.getText().isEmpty();
}
I ended up just using a different way to validate this and got it working