This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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?
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.
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.
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