aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JFormattedTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFormattedTextField" Watch "JFormattedTextField" New topic
Author

JFormattedTextField

Graham Weatherup
Greenhorn

Joined: Jun 21, 2011
Posts: 8
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

Joined: Oct 16, 2003
Posts: 1085
    
  10

One way to achieve is to override stringToValue and return null if the text is empty. ex.:


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Graham Weatherup
Greenhorn

Joined: Jun 21, 2011
Posts: 8
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

Joined: Oct 16, 2003
Posts: 1085
    
  10

It is getting empty for me on focus lost (when I enter alphabets). Did you try my code?
Graham Weatherup
Greenhorn

Joined: Jun 21, 2011
Posts: 8
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

Joined: Oct 16, 2003
Posts: 1085
    
  10

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

Joined: Oct 16, 2003
Posts: 1085
    
  10

My reply and your 3rd edit co-incided.

You're welcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JFormattedTextField