Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFormattedTextField

 
Graham Weatherup
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like my JformattedTextField(NumberFormatter(Numberformat.getIntegerInstance)) but be able to be reset to empty. then return null when I getValue

At the moment it returns to the last valid entry but I would like null to be valid, just not say a double/text. And for this one I do want it to parse a double

I have similar problems for double versions of JFormattedTextField too.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to achieve is to override stringToValue and return null if the text is empty. ex.:
 
Graham Weatherup
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with this is that the text is not empty as soon as focus is lost. (I think by default is commit or revert) this is how i want it to behave for letters in a number field say but not for blank. Any other thoughts?
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is getting empty for me on focus lost (when I enter alphabets). Did you try my code?
 
Graham Weatherup
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I think I understand a bit better now. the stringToValue is in NumberFormatter? I wasn't using this directly I was calling a getFormatter from the subclass so if i override it I'd have to do it in all subclasses? as this has to be done where the Formatter is instantiated?

But yes I see now it does work, Thank you
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graham Weatherup wrote:the stringToValue is in NumberFormatter?

If you click on the NumberFormat link, you can see where the stringToValue method is in API.

Graham Weatherup wrote:
I wasn't using this directly I was calling a getFormatter from the subclass so if i override it I'd have to do it in all subclasses? as this has to be done where the Formatter is instantiated?

If you are sub-classing JFormattedTextField, then you can write this stuff in the constructor. The subclass question is a Java/OOPS question and has nothing to do with formatted text fields. I can't answer for you as I have no idea of your class structure.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My reply and your 3rd edit co-incided.

You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic