JFormattedTextField 显示逗号并接受小数点
JFormattedTextField display commas and accept decimal points
我正在使用 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 在编辑和显示时的格式不同,这是我问题的根源。我找到了一篇关于使用 JFormattedTextField 的非常好的(如果不是旧的)文章,它将所有内容很好地组合在一起。 http://www.drdobbs.com/jvm/java-better-interfaces-via-jformattedtex/224700979
我正在使用 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 在编辑和显示时的格式不同,这是我问题的根源。我找到了一篇关于使用 JFormattedTextField 的非常好的(如果不是旧的)文章,它将所有内容很好地组合在一起。 http://www.drdobbs.com/jvm/java-better-interfaces-via-jformattedtex/224700979