wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Issue with using JFormattedTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Issue with using JFormattedTextField" Watch "Issue with using JFormattedTextField" New topic
Author

Issue with using JFormattedTextField

Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Hello,

I am using JFormattedTextField in my code as follows



The issue I am having here is that when I input any text into the Textfield and then delete it and the focus moves out of the Text field, the previous entered value gets displayed.
Is there any way that I can stop this from occuring.

Please suggest a way to resolve this issue.

Regards
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Please PostRealCode
As per the code you posted, the numberFormat instance you use with the JFormattedTextField is null


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Hi Maneesh,

Is the code



not instantiating the NumberFormat?

Regards
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Your original code was


As you can see numberFormat was null
Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Hi Maneesh,

Its been corrected now. Coudl you please help me with the above issue.

Regards
Nirmal
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155
- right

- JFormattedTextField is overkill for special purposes, for basics in most cases, then is suggested to use plain JTextField with DocumentFilter instead (strongly agree with that)

- but is not required in your case, there is quite simple workaround, by override the (Xxx)Formatter by using your original NumberFormat

- then there are accessible rest or interesting methods, e.g. (Xxx)Formatter#setAllowsInvalid, min, max etc..,

- for example, both versions, is possible to comparing

Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Hi,

Thanks a lot for the sample.

In the TextField with the formatter factory, can we have the text field as empty (not with any value ) by default.

I tried modifying the example but did not work.

Regards
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155
can we have the text field as empty (not with any value ) by default.

- default property for Formatter, against NPE, never ever, don't to set Xxx.setValue(null) or

- simple demostrations about whats happends with InputMask, and by mixing with JFormattedTextField constants

Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Hi,

I have an issue with the above code. I have used the above code as shown here


When I start to type in the numbers 123456 etc, the 0 remains in the Integer position and then the numbers are getting enterd in between.
Is there any thing else that I should be doing here, like use Listeneres for the text field to format the text.

If I start to delete any of the integers, it displaying something else or adds zeroes to the integer part. If all the numbers are deleted the 0.00 gets displayed.

Regards


[JFormattedTextField.jpg]

m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155
- add FocusListener

- override focusGained

- put there something like as (pseudocode)

myTextField.requestFocusInWindow();
myTextField.setText(myTextField.getText());
myTextField.selectAll()

all three code lines wrapped into invokeLater
 
GeeCON Prague 2014
 
subject: Issue with using JFormattedTextField