File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text Filed Problem?

 
lakmal padmakumara
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jeff Yan
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. :

import java.io.*;


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


hope that helped!
 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff:

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

John.
 
Jeff Yan
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very true! i think your right there.
 
Victor Ewert
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic