This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Setting Up a JTextField to only accept Integers - Solution! (with Questions) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting Up a JTextField to only accept Integers - Solution! (with Questions)" Watch "Setting Up a JTextField to only accept Integers - Solution! (with Questions)" New topic
Author

Setting Up a JTextField to only accept Integers - Solution! (with Questions)

Declan Dee
Greenhorn

Joined: May 04, 2010
Posts: 20
Hi,

I was asked during my course to set up a JTextfield to make sure that only integers are accepted. I spent ages on several forums looking for a solution but I could not find anything.

Luckily I manged to find a solution, but unfortunatly I am not 100% what I have typed and I would love to know excatly what it is. Its works perfectly by the way.

You can type only integers and nothing else



Any explination? - Thanks in advance

Good to finally have a solution!

You have no idea how long I was looking for this!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Congratulations on getting it to work!

Integer's parseInt method throws a NumberFormatException if the String parameter cannot be parsed as an int. This is the exception you're catching if the String returned by getText contains something other than digits.

But...

There are ways to do this without throwing an exception. I don't know the "best" way, but one approach is to consider that a String is a sequence of characters, and the Character class has a method isDigit. You could test the user's input and keep asking them to correct it until it satisfies your condition.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Other possible Swing solutions: use a JFormattedTextField instead of a JTextField, or you could use a JTextField and add a DocumentFilter to the PlainDocument that the JTextField holds.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I spent ages on several forums looking for a solution but I could not find anything.

you need to sharpen your research skills

JTextField is a Swing component
the ranch has a Swing Forum
the Swing forum has an FAQ
the FAQ has this listing

NumericTextField - how to create a text field that only accepts numbers as input
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Michael Dunn wrote:... NumericTextField - how to create a text field that only accepts numbers as input

Nice!

(Like I said above, "I don't know the 'best' way...")
Declan Dee
Greenhorn

Joined: May 04, 2010
Posts: 20
Hi,

Of course it can be made even simpler:



However there is one BIG problem. When using this you can enter a minus number and it will be accepted
 
 
subject: Setting Up a JTextField to only accept Integers - Solution! (with Questions)