• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JFormattedTextField using NumberFormatter ?

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will move this to the swing forum.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of a formatted textfield:
1) JSpinner
2) JTextField with DocumentFilter
 
Can Zheng
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic