Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFormattedTextField using NumberFormatter ?

 
chad stevens
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will move this to the swing forum.
 
Can Zheng
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic