File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Text Filed Problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Text Filed Problem?" Watch "Text Filed Problem?" New topic

Text Filed Problem?

lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
I have a Text Field in my Jframe. Is there any Exception which throws it self when we called textfiled.getText() without entering anything in it .
What I want to do is if the user does not input anything on the text field and press "OK "button it should throw an Exception.Is there any way to do this ?

[Bsc in Computer Science and Engineering] [SCJP 80%][BCS certificate level]
Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
id use an IOException! what is your code like?
you need to put try before you catch the exception or declare it at the beggining after the method by saying throws IOException e.g. :


public void readFile() throws IOException
//do whatever it is that you do in your method
}catch (IOException e)
System.out.println("Unknown error has occured!");

hope that helped!

~ Jeff Yan ~
John de Michele

Joined: Mar 09, 2009
Posts: 600

Actually, IOException is better used in instances where you're dealing with system IO. IllegalArgumentException is probably a better fit.

Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
very true! i think your right there.
Victor Ewert

Joined: May 28, 2009
Posts: 17
If you are simply trying to let the user know they must enter something into the TextField, then when you use getText(), check if the length of the returned String is > 0, and if it isn't then display a warning message to the user.
I agree. Here's the link:
subject: Text Filed Problem?
It's not a secret anymore!