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

JFormattedTextField using NumberFormatter ?

chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
I have a textfield that is supposed to accept numbers from 1 to 10000. The problem I am facing is that if I do not put a setAllowsInvalid to true, I can type garbage in the textfield and I don't want that. I have also set a min and max value, min being 1, max being 10000. The problem is, when I enter something in the textfield, I want to be able to erase what is there first and then enter my new value instead of having to overwrite the existing values, that means I want to be able to backspace to an empty textfield and, not be able to enter characters other than numbers between 1 and 10000. Right now, when I backspace, it doesn't let me eliminate the last character and also when I try to enter a new value, I need to place the cursor at the front of the textfield and overwrite the characters. I just want to be able to erase what is there, with mouse, delete or backspace and see it empty, then enter my new value.
A quick response would be very much appreciated.
Thanks to all.
- Chad
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
I will move this to the swing forum.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Can Zheng

Joined: Jul 15, 2006
Posts: 16
Sorry to dig out this pretty old post. But I'm having exactly the same problem and can't work it out.

Anyone can shed some light on this?


Best regards,<br /> <br />Can Zheng
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
instead of a formatted textfield:
1) JSpinner
2) JTextField with DocumentFilter
Can Zheng

Joined: Jul 15, 2006
Posts: 16
Hi Micheal, thanks for your advice.

But I still don't understand why can't we use JFormattedTextField with NumberFormatter? I thought deleting all the content could be a usual senario, right? Anyone can help?
I agree. Here's the link:
subject: JFormattedTextField using NumberFormatter ?
jQuery in Action, 3rd edition