Why is JFormattedTextField evil? [closed]
Asked Answered
O

3

18

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?

Overbalance answered 24/8, 2009 at 1:39 Comment(0)
W
15

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).

Wendelin answered 24/8, 2009 at 2:55 Comment(0)
M
9

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.

Monseigneur answered 24/8, 2009 at 4:47 Comment(1)
The main problem with JFormattedTextField is that it is optimised for a rare use case, instead of a common one, while a component for the common task is missing. AWT is bad enough, but Swing, again, has been designed by people who had no clue about the state of the art in GUI programming, and making the same mistakes twice it really bad! If the designers of Java and Swing had just looked at what Delphi or NeXt's Interface Builder were able to do since early in the 1990's and how they did it, both the language and Swing would be much easier to use!Prelature
R
7

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.

Relax answered 4/2, 2010 at 9:13 Comment(1)
Related question: #2206871Relax

© 2022 - 2024 — McMap. All rights reserved.