Search code examples
javajtextfield

JTextfield, how to verify content in the getText() method


I have a textfield called x.

When the textfield contains " ", I want to do something. If it does not, do something else.

I tried doing

String test = x.getText();
if(test.startsWith(" ")){buttonN.setForeground(Color.GRAY));}
else{buttonN.setForeground(Color.BLACK));}

but it didnt work. any suggestions


Solution

  • (Color.GRAY)) and (Color.BLACK)) end with 2 closing parenthesis, while only one was opened.

    String test = x.getText();
    if (test.startsWith (" "))
    { 
         buttonN.setForeground (Color.GRAY); 
    }
    else buttonN.setForeground (Color.BLACK);
    

    Some spaces around parenthesis make the reading more convenient.