JFormattedTextField 显示逗号并接受小数点
Posted
技术标签:
【中文标题】JFormattedTextField 显示逗号并接受小数点【英文标题】:JFormattedTextField display commas and accept decimal points 【发布时间】:2018-11-24 21:26:53 【问题描述】:我使用JFormattedTextField
输入带小数点的数字。以下是格式化程序。
public static AbstractFormatterFactory createFormatter()
final NumberFormat format = NumberFormat.getInstance();
final InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setValueClass(Double.class);
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
formatter.setCommitsOnValidEdit(true);
return new DefaultFormatterFactory(formatter, formatter, formatter);
setAllowsInvalid(false)
将忽略句点,但根据需要每 3 位添加逗号。接受setAllowsInvalid(true)
句点,但在焦点更改之前不会显示逗号。
有没有办法同时拥有这两种行为?
【问题讨论】:
可以从 JFormattedTextField 创建自定义扩展 我有,但是你建议扩展类做/覆盖什么? 是的,您可以重写该方法并创建类似的逻辑以同时支持这两种方法.. 你指的方法是什么?允许无效的检查在格式化程序中并使用私有变量。 【参考方案1】:我相信 JFormattedTextField 在编辑和显示时的格式不同,这是我问题的根源。我找到了一篇关于使用 JFormattedTextField 的非常好的文章(如果不是旧文章),它将所有内容很好地组合在一起。 http://www.drdobbs.com/jvm/java-better-interfaces-via-jformattedtex/224700979
【讨论】:
以上是关于JFormattedTextField 显示逗号并接受小数点的主要内容,如果未能解决你的问题,请参考以下文章
如何让 JFormattedTextField 使用 1.55 美元,但将值存储为整数 155?