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?
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?
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).
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.
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.
© 2022 - 2024 — McMap. All rights reserved.