wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes TextFields Input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TextFields Input " Watch "TextFields Input " New topic
Author

TextFields Input

Ramgopal Reddy
Greenhorn

Joined: Jan 28, 2006
Posts: 22
Hai am wanna like to know about TextFields Input type.
Now i would like to give input as a numbers.
So i don't want allow alphabets in that field how its possible?
plz send sample code to me.


Ram's
Kristof Janssens
Greenhorn

Joined: Jan 05, 2005
Posts: 23
You could have used google to find this one ... did you even search?

anywhy, here you find an example:
http://www.java2s.com/Code/Java/Swing-JFC/Textfieldonlyacceptsnumbers.htm


SCJP 1.4 (90%)<br />SCJD (in progress)
Ramgopal Reddy
Greenhorn

Joined: Jan 28, 2006
Posts: 22
Thans Dear Friend,
But i would like to Develop that aplication with AWT.
If possible tell me buddy!
Bruce Xu
Greenhorn

Joined: Feb 09, 2006
Posts: 7
I prefer using JFormattedTextField.
Hopefully following can give u some hints.
----------------------------------------
import javax.swing.JFormattedTextField
import javax.swing.text.DefaultFormatterFactory;
import java.text.NumberFormat;
import javax.swing.text.NumberFormatter;
...
NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumIntegerDigits(3);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setAllowsInvalid(false);
formatter.setMinimum(new Integer(0));
formatter.setMaximum(new Integer(255));
...
------------------------------------------
If you need more flexible&custmozied requirements, you probably need to overwrite some functions of "Formatter" or create you owner "Format" class.

here is the official tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html
B Chen
Ranch Hand

Joined: Dec 27, 2005
Posts: 89
Originally posted by Ramgopal Reddy:
Thans Dear Friend,
But i would like to Develop that aplication with AWT.
If possible tell me buddy!


Check your instructions. I believe you must use Swing for all your GUI components.
Another way to do it is to add KeyListener to JTextField to check the input character. Consume the event if the character is not a digit.

[ March 17, 2006: Message edited by: B Chen ]
[ March 17, 2006: Message edited by: B Chen ]

SCJD<br />SCJP 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Moving to Swing forum. Please keep this forum for SCJD topics.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: TextFields Input
 
Similar Threads
heavy addRow problems in a jtable
Parsing Strings in Midlet
how do I change the focus?
How To Set All the Textfields in a Web Page to READONLY?
facing problem in event handling