Results 1 to 4 of 4
  1. #1
    drgnfire25 is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default make validator change text field color

    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?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,638
    Rep Power
    12

    Default

    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)

  3. #3
    drgnfire25 is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    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:

    Java 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();
                 }

  4. #4
    drgnfire25 is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    I ended up just using a different way to validate this and got it working

Similar Threads

  1. Applet to change text color
    By sak_java in forum Java Applets
    Replies: 8
    Last Post: 02-19-2011, 06:56 PM
  2. Change text color on type
    By corbokhan in forum New To Java
    Replies: 2
    Last Post: 11-01-2010, 07:30 PM
  3. change syntax text color
    By dejos456 in forum New To Java
    Replies: 5
    Last Post: 12-14-2009, 11:09 AM
  4. How to make a text field to accept numbers only?
    By Robert_85 in forum Advanced Java
    Replies: 15
    Last Post: 04-07-2009, 01:30 PM
  5. Replies: 0
    Last Post: 06-25-2008, 10:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •