tags:

views:

835

answers:

6

Hi,

In this question Is there any way to accept only numeric values in a JTextField? one of the answers suggested that JFormattedTextField had issues.

I've not yet used it, but could somebody please expand (or disagree) on the issues with this class?

Thanks...

+6  A: 

JFormattedTextField is evil because of poor usability. For instance, if a text field should only be able to accept an integer value, JFormattedTextField will allow any old rubbish and then complain when focus is lost. To make matters worse, it has a range of configurable behaviours when focus is lost (a clear sign that none of them is the correct choice).

It also has "k3wl" formatter selection. It also seems to have poor interaction with the document (it tries to install a DocumentFilter, but that is not part of the Document interface).

Tom Hawtin - tackline
+5  A: 

I'll admit JFormattedTextField is not the easiest class to use, and yes it does have issues but like most components it does a reasonable job at some things and a bad job at others.

It does in fact allow you to edit for Integer values as the data is typed when you use a MaskFormatter. So for simple tasks it can be effective.

Editing of data is a complex task. Sometimes you need to edit the data as it is entered (in the case of checking for Integer values). Sometimes you need to edit the data after it is entered (in the case of validating a date). Then of course you need to decide how to handle invalid data.

There can be a certain amount of configuring the parameters when doing non-trivial tasks. They may not always work the way you want them to or the way you expect them to so you do need to experiment to find out when you can/should use this component.

camickr
+1  A: 

Unfortunately, I saw the class and thought what a great idea! I think I'll end up rolling my own during the next iteration of this app.

I currently have a field with a phone number formatter and when I try to setText("") or setText(" ") to clear it out (as done when it was an instanceof JTextField), I get "invalidEdit()". If I override setText() to check for these and then call setValue(null), I get a stack overflow for obvious reasons.

The problem is that the formatter doesn't like " ". I just want to clear the fricken value for chrissake! There is no reset(), clear(), empty() or kabosh() method. Before I end up with SwingUtilities.invokeLater(new Runnable(){public void run(){setValue(null);}}); I'll check the MaskFormatter doc.

No dice - even though the doc says an invalid value will set text to empty, it doesn't actually do that. jeezis, this has been so much more trouble than it's worth!

Something I find typical in Java: this overblown pseudo-OO ends up making the simple things HARD instead of making the hard things simple. Whoever worked on this probably also invented IOC...

GARRRGGGHHHHHHHH!!

mordekai smith
A: 

JFormattedTextField is not evil. You can use a JFormattedTextField with a MaskFormatter to limit TextField input to number values only. I'm not sure what problems you had with the JFormattedTextField, but I've used them along with a MaskFormatter without any problems. The MaskFormatter takes care of everything and it will not accept or display any character that is not a number.

mjh2007
+3  A: 

I'm struggling with JFormattetTextField for the moment. I'm trying to use NumberFormat.getPercentInstance() for formatting my percent-value.

The worst thing is that .getValue() doesn't return the same value as the user is seeing. In example if I type 25,5% in the formatted textfield, then it rounds it to 26%, but .getValue() returns 0.255. That is Evil.

Jonas
Related question: http://stackoverflow.com/questions/2206371/how-to-get-the-same-value-as-the-user-is-seeing-from-a-jformattedtextfield
Jonas
A: 

I am seeing an issue with JFormatTextField. I want to override the existing value to empty/null. But it never allows me to do. If delete the entire value and tab on to next field in the pane, the old value appears. So we can never clear or reset the JFormatTextField!

If you have a solution for it, please share with me.

Thanks, Sri

Sridhar
Hello, welcome to Stack Overflow. Please review the FAQ (http://stackoverflow.com/faq) to learn more about how this site works. This site is not a discussion board, this is a place for questions to be asked and answered. As such, you should not post a new answer if what you want to say doesn't actually answer the question. If you want to ask a new question, do so by clicking the "Ask Question" button.
Gnoupi