• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About JTextFiled

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use the == operator to compare the contents of two Strings. Use the equals() method of String instead.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also skip the check against null - JTextField.getText() never returns null.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic