-
我有一个JFormattedTextField
它只接受 8 位数字,但是当我尝试使用退格按钮清除文本字段时,它不会删除数字的第一个字符(与删除按钮的行为也相同),我每次都必须预先使用 Esc 键来删除该字符。
NumberFormat intFormat = NumberFormat.getIntegerInstance();
intFormat.setGroupingUsed(false);
NumberFormatter numberFormatter = new NumberFormatter(intFormat);
numberFormatter.setValueClass(Integer.class);
numberFormatter.setAllowsInvalid(false);
numberFormatter.setMinimum(0);
numberFormatter.setMaximum(99999999);
releaseNoTextField = new JFormattedTextField(numberFormatter);
这里有什么问题?
- 还可以清除此文本字段
releaseNoTextField.setText("")
不起作用,还有其他方法可以做到这一点吗?
我有一个仅接受 8 位数字的 JFormattedTextField,但是当我尝试使用退格按钮清除文本字段时,它不会删除数字的第一个字符(与删除按钮的行为也相同),我必须预先使用 Esc 键来删除每个字符时间。
这是应用的限制numberFormatter.setAllowsInvalid(false);
看到一个空白String
(""
) 不是有效的数值。如果你使用numberFormatter.setAllowsInvalid(true);
您可以删除所有字符,但也可以输入您喜欢的任何值。
不过,该字段将在用户离开该字段后进行验证。
如果您不关心已格式化的值(12, 345, 678
),那么你可以使用DocumentFilter
应用于平原JTextField
在其中实现您需要的逻辑。看实现文档过滤器 and 文档过滤器示例更多细节。
另外,使用 releaseNoTextField.setText("") 清除此文本字段不起作用,还有其他方法可以做到这一点吗?
你应该(总是)使用setValue
for JFormattedTextField
你应该是真的setValue(null)
清理场地
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)