aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes About JTextFiled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "About JTextFiled" Watch "About JTextFiled" New topic
Author

About JTextFiled

Akshay J Patil
Greenhorn

Joined: May 08, 2013
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Don't use the == operator to compare the contents of two Strings. Use the equals() method of String instead.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2419
    
    7

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Akshay J Patil
Greenhorn

Joined: May 08, 2013
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: May 08, 2013
Posts: 4


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

Joined: Jun 13, 2009
Posts: 2182
    
    7


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

You want:



Akshay J Patil
Greenhorn

Joined: May 08, 2013
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

You can also skip the check against null - JTextField.getText() never returns null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: About JTextFiled