Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About JTextFiled

 
Akshay J Patil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all...
can anyone please help me..?

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.

thank you.
 
Paul Clapham
Sheriff
Pie
Posts: 20959
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use the == operator to compare the contents of two Strings. Use the equals() method of String instead.
 
K. Tsang
Bartender
Posts: 3397
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

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.
 
Akshay J Patil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(text.equals(???))
{
System.out.println("ERROR");
}

what to pass in place of '???'...
because i tried passing null, tried passing "", tried passing " ".., all in vain... :/
 
Paul Clapham
Sheriff
Pie
Posts: 20959
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Akshay J Patil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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...
 
Rob Camick
Ranch Hand
Posts: 2564
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


"text" is the variable for the JTextField. You can't compare it to a String.

You want:



 
Akshay J Patil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also skip the check against null - JTextField.getText() never returns null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic