aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JFormattedTextField problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFormattedTextField problem" Watch "JFormattedTextField problem" New topic
Author

JFormattedTextField problem

Alex Chard
Greenhorn

Joined: Apr 05, 2011
Posts: 6

Hi all

My first post here, and I'm relatively new to Java programming, although I've been an Oracle developer for over a decade now.

On to my problem.

I need to create a field on a form that only allows the user to input whole numbers (integers).

I've implemented this as follows:

This works in the sense that it only allows integer values in the field, and along with other code, does just about everything I need.
The only problem I have is changing the number entirely. The numberFormatter will not let me delete the last character in the field.

E.g.
If the initial value is 600,000, and I delete the 6, the field shows 0. I cannot delete this zero so that the field is empty, which I need to do.
If I delete the zeros one after the other, I am left with a 6 that I can't delete.

This is not good, because to update the field from 600,000 to 12 (for example), I have to do the following:
1. Delete all the zeros, leaving the 6.
2. Enter 12, meaning the value in the field is now 612, or 126 depending on how the initial value was modified by the user.
3. Delete the 6.

I've looked at the NumberFormatter and IntegerFormatter classes and documentation but can't see anything that allows the field to contain blank values.

Is there an option to allow an IntegerFormatter to have a blank field?
If not, can anyone suggest another way of doing this?

Thanks

Alex
Alex Chard
Greenhorn

Joined: Apr 05, 2011
Posts: 6

OK, so a bit more investigation shows the following.

Firstly, the code snippet above is wrong. It should be:



The setAllowsInvalid line of code is what causes the behaviour I was seeing.
However, removing that means that any value can be entered in to the field, and it is only validated when the field is exited. Any invalid characters are then simply stripped from the text in the field.
The problem here is that the user can click the Next button on my form, and not notice the invalid characters.

I'm after a halfway house of the above behaviour, where an invalid character can't be entered, but a blank string can.

Any ideas appreciated.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
You can use a regular text field with a Document Filter to validate the characters as they are entered. However you lose the formatting of the thousands character in your text field.
Alex Chard
Greenhorn

Joined: Apr 05, 2011
Posts: 6

Thanks for the reply. Next up is trying a Document Filter, but I was hoping to keep to a FormattedTextField.
Alex Chard
Greenhorn

Joined: Apr 05, 2011
Posts: 6

Just to let you all know I sorted this by using the NumberFormatExt NumberFormat from the SwingX project.
This handles the blank fields, which the default NumberFormat won't allow.

Hope that helps someone.
Steve Sugden
Greenhorn

Joined: Aug 20, 2005
Posts: 8
I know this is an old thread but I thought I'd share my solution. I'd never heard of JFormatedTextField before and was disapointed by this obvious bug. So I took a quick look at the source code and decided to override the processFocusEvent method. It checks that the 'text' is empty and if it is calls setValue(null). This seems to work as I presume null passes the validation check.




Hope this helps someone.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
Steve Sugden wrote:I know this is an old thread but I thought I'd share my solution. I'd never heard of JFormatedTextField before and was disapointed by this obvious bug. So I took a quick look at the source code and decided to override the processFocusEvent method. It checks that the 'text' is empty and if it is calls setValue(null). This seems to work as I presume null passes the validation check.




Hope this helps someone.


I am using the netbeans IDE, I want to use a "postmortem" method of adding processFocusEvent to JFormattedTextField, hence after it has been created, rather than on the fly.
How would I do this? I tried to addFocusListener but there isn't anything to override
Thanks
Jack
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2373
    
    7

Besides the Document Filter, you may want to look into DocumentListener. This listener can allow you to customize the validation as user types the input.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
You may want to look into DocumentListener. This listener can allow you to customize the validation as user types the input.


No. A DocumentFilter does this.

A DocumentListener notifies you AFTER the text has been added to the Document. So the validation can only be after typing, not during typing.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
So how do I add DocumentListener or DocumentFilter whatsoever to a JFormattedTextField in the constructor of a JFrame since the initComponents() method cannot be modified.
Thanks
Jack
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2373
    
    7

Well for DocumentListener you can


For DocumentFilter you do similar but need casting


Here AbstractDocument is a concrete class of Document interface and can also be DefaultStyleDocument, HTMLDocument, PlainDocument.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
K. Tsang wrote:Well for DocumentListener you can


For DocumentFilter you do similar but need casting


Here AbstractDocument is a concrete class of Document interface and can also be DefaultStyleDocument, HTMLDocument, PlainDocument.


Dear Tsang,
Sorry for being insistent, but how do you expose processFocusEvent of JFormattedTextField from the constructor?
I don't understand how to write a document listener.
Thanks
Jack
Vismaya Madhu
Greenhorn

Joined: Sep 04, 2013
Posts: 2
hai.. I am a new user .. need your help.. please give me codes to retrieve text file into a text area in a jframe when select anode of tree.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFormattedTextField problem