Java:JFormattedTextField 更改 focusLost 上的 setGroupingUsed()获得焦点
我有许多 JFormattedTextdields ,其中包含数字数据。我使用过 DecimalFormat、InternationalFormatter、DocumentListener 并尝试使用 CaretPositionListener。
我面临的唯一问题是当数字输入增加并且分组字符出现在中间时插入符号的位置。
如何在 focusGained 和 focusGained 上动态设置各自 jformattedtextfields 的 DecimalFormat 的 setGroupingUsed() onFocusLost。
任何想法或建议......
更新代码&问题: 当我尝试输入“12345”时,添加“1234”时会出现一个逗号“1,234”。这会将插入符号置于 3 和 3 之间。 4 而不是 4 之后。我使用的格式化代码:
DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMinimumFractionDigits(2);
numberFormat.setRoundingMode(RoundingMode.HALF_UP);
final InternationalFormatter formatter = new InternationalFormatter(numberFormat);
formatter.setAllowsInvalid(false);
formatter.setMinimum(0.00);
formatter.setMaximum(999999999.99);
return formatter;
这是我在自定义 JFormattedTextField 上实现的 解决方案。您是否有更好的方法来处理输入值时的分组字符,以便插入符号位置保持正确:
public void focusGained(FocusEvent e) {
if (numberFormat.isGroupingUsed()) {
Object o = this.getValue();
numberFormat.setGroupingUsed(false);
formatter.setFormat(numberFormat);
this.setFormatterFactory(new AbstractFormatterFactoryImpl());
this.setValue(o);
this.setText(o.toString());
}
}
public void focusLost(FocusEvent e) {
try {
this.commitEdit();
} catch (ParseException ex) {
//Logger.getLogger(NumberFormattedTextField.class.getName()).log(Level.SEVERE, null, ex);
}
Object o = this.getValue();
//System.out.println("focusLost : getValue = " + o);
numberFormat.setGroupingUsed(true);
formatter.setFormat(numberFormat);
this.setFormatterFactory(new AbstractFormatterFactoryImpl());
this.setValue(o);
this.setText(o.toString());
//System.out.println("focusLost : Text Set = " + o.toString());
}
I have many JFormattedTextdields with number data in it. I have used DecimalFormat, InternationalFormatter, DocumentListener and also tried with CaretPositionListener.
The only problem I face is caret position when number input grows and grouping character comes in between.
How can I dynamically set setGroupingUsed() of DecimalFormat of respective jformattedtextfields on focusGained & onFocusLost.
Any ideas or suggestions....
UPDATE CODE & PROBLEM :
When I try to input "12345", on adding "1234" a comma appears "1,234". This brings to caret between 3 & 4 instead of after 4. The Formatting code I use :
DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMinimumFractionDigits(2);
numberFormat.setRoundingMode(RoundingMode.HALF_UP);
final InternationalFormatter formatter = new InternationalFormatter(numberFormat);
formatter.setAllowsInvalid(false);
formatter.setMinimum(0.00);
formatter.setMaximum(999999999.99);
return formatter;
This is what I have implemented as SOLUTION on a custom JFormattedTextField. Do you have any better approach to handle the grouping character while inputting value, so just caret position remains proper :
public void focusGained(FocusEvent e) {
if (numberFormat.isGroupingUsed()) {
Object o = this.getValue();
numberFormat.setGroupingUsed(false);
formatter.setFormat(numberFormat);
this.setFormatterFactory(new AbstractFormatterFactoryImpl());
this.setValue(o);
this.setText(o.toString());
}
}
public void focusLost(FocusEvent e) {
try {
this.commitEdit();
} catch (ParseException ex) {
//Logger.getLogger(NumberFormattedTextField.class.getName()).log(Level.SEVERE, null, ex);
}
Object o = this.getValue();
//System.out.println("focusLost : getValue = " + o);
numberFormat.setGroupingUsed(true);
formatter.setFormat(numberFormat);
this.setFormatterFactory(new AbstractFormatterFactoryImpl());
this.setValue(o);
this.setText(o.toString());
//System.out.println("focusLost : Text Set = " + o.toString());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢,
我通过创建一个自定义文本字段来管理我需要的一切来解决。任何改进建议。代码已添加到上面的问题中。
Thanks,
I solved by creating a custom textfield that manages everything I need. Any suggestion for improvements. Code is added above in question.