there is one text field in my UI.., and one Jbutton...
when user, without entering anything in text field, cliks the button, i want to display an error...
how do i do this?
following does not work:
if(text.getText==null || text.getText == "" || text.getText == " ") //text is an object of JTextField class
{
System.out.println("ERROR");
}
please can anyone tell me how to generate error if nothing is entered in text field.
Checking for null is one part. The other part is use the equals() method rather than == operator.
And you need to check the JTextField object is null or not, not after getText, because if the text object is null you won't even able to do getText and the program will throw NullPointerException.
Okay. So now it's your turn to explain what you did, what you expected to happen, and what happened instead. Read our FAQ entries TellTheDetails and ItDoesntWorkIsUseless (<-- click on those links). And post the code you actually ran, not a quickly-typed approximation of it.
this is the code...
i use eclipse...
JDK 6...
WHAT I WANT: i want the error message to be displayed, when user doesnt enter anything in text field...
WHAT IS HAPPENING: when the code is executed, if condition is never satisfied after button click.., else block is executed always...
thanks Rob ...
its just been a week since i've started learning java.., so as of now.., m a bit weak at it... thanks all for bearing with me...
i appreciate the help...