Why is JFormattedTextField evil? [closed]

时间秒杀一切 提交于 2019-12-17 18:39:23

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1320117/why-is-jformattedtextfield-evil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!