• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to catch number errors?  RSS feed

 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..

i have 3 textfields..
these 3 textfields only allow text to be typed in..

if user type in numbers, it will give an error msg, informing user only text are allowed..

what is the simplest way to create an error msg under "g.drawString" to inform users?


for eg: g.drawString ("Error messgage: Please enter texts only.");
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subject: how to display error messages
Yes you can use the drawString method to display messages, but there are other better ways to do it. However for a beginner, I think you should experiment with various simple ways of doing things before moving to the more complex classes.
A 5 cent tutorial. The drawString method is usually used in a paint() method for a component. When you layout your window/frame you will need to have a component in which you will display your error message. Override that component's paint() method and use drawString() to output your message.
You can control when the paint() method will be called (somewhat) by telling the AWT system that the component needs to be repaint()ed (that's the method).
Some time after you call repaint() the AWT system will call the component's paint() method. There is more to it than this, and you should read in the Java Tutorial about it.

Here's a simple program with an example:

Try making changes to this to see what happens.
 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello..

thanks for ya responses.. erm somehow i dont really understand the coding u gave me as refernce..

how can these coding catch numbers error??

btw i got some errors when i compiled e coding u typed.
tt why i cant see the outcome of it..
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May, are you asking how to detect if the user has typed in a string that is not a number? You can do this:




Some things to think about after you're comfortable with this:

1) Use dialogs to display error messages. See java.awt.Dialog.

2) The best gui design never lets the user enter invalid data. You can subclass TextField so that typing non-number characters has no effect.


-- Phil
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only question I saw in your posting was:

what is the simplest way to create an error msg under "g.drawString" to inform users?

That was the one I answered.

What error(s) did you get when compiling my program?

One error was probably because I left a testing reference.
Change the reference to NormsTools.TestFrame to Frame.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by may tan:
Hello..

thanks for ya responses.. erm somehow i dont really understand the coding u gave me as refernce..

how can these coding catch numbers error??

btw i got some errors when i compiled e coding u typed.
tt why i cant see the outcome of it..


If you are having a difficult time understanding Norm's example code, you may be jumping in a little too far over your head. Perhaps you should post the code that you have already so that we can lead you in the right direction from there.

Also, the Java API provides a JFormattedTextField that checks ther user's entry into a text field for the proper formatting. You can easily configure it for numerical data entry. You can probably use the isEditValid() value to detect if the user entered valid data and respond with an error message. Of course, this leads back to your original question: how to display an error message.

While your suggestion to use g.drawString() is valid, it is somewhat limited since the drawString() method is only available in the paint() method of some component. Also, for GUI applications, this is not very standard. Most applications display error messages in a separate dialog box. Java provides the JOptionPane class to help programmers quickly display standard dialog boxes. I suggest that you check out the How to Make Dialogs from Sun's Java Tutorial for an introduction to creating dialogs with JOptionPane.

In fact, this tutorial contains a lot of good information to start learning about different aspects of the Java language and the APIs that are available. I strongly encourage you to bookmark it and use it for reference as you continue to learn Java. You should also bookmark and learn to navigate the API documentation for the version of Java you are using. The links I gave above for JFormattedTextField and JOptionPane come from the documentation for Java 1.5, so if you are using an earlier version, you should look for the docs for the version you are using.

I hope this helps answer your question. If you still need help, please let us know.

Layne
 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello pple..
thanks a lot for your responsese..


i'm still kind of lost because i never use errors detecting before..
so i dont have any coding that i can post here for u all to check..
hmm.. can i rephase my qns again??
then u all try help me out from there..


ok, i have 3 textfield that only ALLOWED the following colors.
[black, brown, red, orange, yellow, green, blue, violet, grey, white]

--------------------------------------------------------------------------
but if users enter "pink" or some other funny text like "haha" in any of the textfields, i want an error msg appearing below my applet as g.drawString stating:
"sorry, please enter a valid color."


and also,
if user enter any number in any of the textfield, it also gives an error msg as g.drawString stating:
"sorry, please enter a valid color."
--------------------------------------------------------------------------

Please help me with the coding.. i'm really stuck here..
Thanks =)
[ August 13, 2005: Message edited by: may tan ]
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like there are two problems here:
1) how to detect an error
2) how/where to post a message.

My previous post had code for the second. I'm sorry that I forgot to remove my TestFrame class so it would't compile. The class has these statements that you could put in the constructor of the program to get it to work
Try to solve the problems separately by writing a small, simple test program that works only on that problem. When you solve it, then you can copy the correctly working techniques into the large program.
Putting all the problems at the same time into one program can make solving any one of them more difficult.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the user to select from a given list of choices, a JTextField is the wrong control to use. You should use a JComboBox instead. That way you don't have to worry about any error checking.

Layne
 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erm.. actually i'm only suppose to use text field for user inputs..

Basically e problems is whenever the user enter number, other texts or colors not stated, it will give an error message in g.drawString.

"sorry, please enter a valid color."

i just need a simple coding for showing of error message whenever users key in numbers, other texts or colors not stated..

Can anyone show me what is the coding to it??


oh btw, i have already added.. actionListener for button, i wont be able to add windowListener right?
[ August 15, 2005: Message edited by: may tan ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!