java 如何让 JFormattedTextField 接受没有小数点(逗号)的整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14467858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make JFormattedTextField accept integer without decimal point (comma)?
提问by Saleh Feek
I used JFormattedTextField
withNumberFormat
in this way:
我JFormattedTextField
以NumberFormat
这种方式使用:
-Creat a JFormattedTextField
refernce
-Creat一个JFormattedTextField
refernce
JFormattedTextField integerField;
-Create a NumberFormat
refernce
-创建NumberFormat
refernce
NumberFormat integerFieldFormatter;
-In the constructor:
- 在构造函数中:
integerFieldFormatter = NumberFormat.getIntegerInstance();
integerFieldFormatter.setMaximumFractionDigits(0);
integerField = new JFormattedTextField(integerFieldFormatter );
integerField.setColumns(5);
..........
.....
I meant to use it with integer numbers only, but when I type numbers like 1500 it is converted after losing focus to 1,500 , and exception thrown this is the first line of it:
我打算只将它与整数一起使用,但是当我输入像 1500 这样的数字时,它会在失去焦点后转换为 1,500 ,并且抛出异常,这是它的第一行:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1,500"
线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“1,500”
When I use JTextField
instead of JFormattedTextField
All integers accepted normally, But the reason why I want to use JFormattedTextField
is to benefit from its input restriction advantages.
当我使用JTextField
而不是JFormattedTextField
正常接受的所有整数时,但我想使用的原因JFormattedTextField
是受益于它的输入限制优势。
采纳答案by Saleh Feek
I discovered the solution to my problem; Hereit is:
我发现了我的问题的解决方案;这是:
The exact problem is that when I use JFormattedTextField
with NumberFormat
, the JFormattedTextField addscomma ',' before any next 3 digits for example
确切的问题是,当我使用JFormattedTextField
with 时NumberFormat
,JFormattedTextField在任何下一个 3 位数之前添加逗号 ',' 例如
1000 rendered as 1,000
10000 rendered as 10,000
1000000 rendered as 1,000,000
1000 渲染为 1,000
10000 渲染为 10,000
1000000 渲染为 1,000,000
When I read an integer value from JFormattedTextField usign this line of code
当我从 JFormattedTextField 读取整数值时,使用这行代码
int intValue = Integer.parseInt(integerField.getText());
The comma is read as part of the string; 1000 read as 1,000 and this string value cannot be converted to integer value, and so exception is thrown.
逗号作为字符串的一部分被读取;1000 读作 1,000 并且此字符串值无法转换为整数值,因此抛出异常。
Honestly the solution is in this Answerbut I will repeat it here
老实说,解决方案在这个答案中,但我会在这里重复
use str.replaceAll(",","")
利用 str.replaceAll(",","")
int intValue = Integer.parseInt(integerField.getText().replaceAll(",", ""));
This will replace any comma charachter ','
in the returned string and will be converted normally to int
as expected.
这将替换','
返回字符串中的任何逗号字符,并将正常转换int
为预期。
Regards
问候
回答by GrafWampula
I realize this is an old question but I just stumbled upon it through the same issue. As the other answers seemed like workarounds to me, I took a closer look at the NumberFormat methods.
我意识到这是一个老问题,但我只是通过同一个问题偶然发现了它。由于其他答案对我来说似乎是解决方法,因此我仔细研究了 NumberFormat 方法。
I found that the easiest approach would actually be to simply deactivate grouping on the NumberFormatinstance:
我发现最简单的方法实际上是简单地停用 NumberFormat实例上的分组:
NumberFormat integerFieldFormatter = NumberFormat.getIntegerInstance();
integerFieldFormatter.setGroupingUsed(false);
That way no group delimiters will appear in the textfield output.
这样,文本字段输出中将不会出现组分隔符。
Of course you will also not be able to use them for your input, but that was not intended by the question, right?
当然,您也无法将它们用于您的输入,但这不是问题的意图,对吗?
Also for an integer instance of NumberFormat you don't need to explicitly setMaximumFractionDigits(0), as that is part of what getIntegerInstance() does for you.
同样对于 NumberFormat 的整数实例,您不需要显式 setMaximumFractionDigits(0),因为这是 getIntegerInstance() 为您所做的一部分。
回答by BackSlash
You can do it in (at least) 2 ways:
您可以通过(至少)两种方式做到这一点:
- Using a keyListener
- Using DocumentFilter
- 使用 keyListener
- 使用文档过滤器
if you want to use KeyListener:
如果你想使用 KeyListener:
KeyListener listener = new KeyAdapter(){
public void keyTyped(KeyEvent e){
if(e.getKeyCode()<KeyEvent.VK_0||e.getKeyCode()>KeyEvent.VK_9{//input<'0' or input>'9'?
e.consume();//delete the typed char
}
}
}
yourTextField.addKeyListener(listener);
to use the DocumentFilter check this link: How to allow introducing only digits in jTextField?
要使用 DocumentFilter,请检查此链接:如何仅允许在 jTextField 中引入数字?
EDIT:i forgot to say this. As MadProgrammer said in the first comment to this answer, KeyListener is not the proper way to do it, because
编辑:我忘了说这个。正如 MadProgrammer 在对此答案的第一条评论中所说, KeyListener 不是正确的做法,因为
You do not know in what order KeyListeners will be notified of the event and the key may have already gone to the text component before it reaches you (or it could have been consumed before it reaches you)
您不知道 KeyListeners 将以何种顺序收到事件通知,并且密钥可能在到达您之前已经进入文本组件(或者它可能在到达您之前已被使用)
EDIT #2: ANOTHER QUICK WAY
编辑 #2:另一种快捷方式
MaskFormatter formatter = new MaskFormatter("#####");
JFormattedTextField field = new JFormattedTextField(formatter);
And the trick should be done. with this you can insert up to 5 digits in tour textfield, more '#' in the string parameter for the formatter = more digits can be typed by the user
诀窍应该完成。有了这个,您可以在游览文本字段中插入最多 5 位数字,在格式化程序的字符串参数中添加更多“#”= 用户可以输入更多数字
回答by Vaibhav Nandkule
Try this, This is a complete solution for creating and validating number JTextField
试试这个,这是创建和验证数字的完整解决方案 JTextField
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);//Remove comma from number greater than 4 digit
NumberFormatter sleepFormatter = new NumberFormatter(format);
sleepFormatter.setValueClass(Integer.class);
sleepFormatter.setMinimum(0);
sleepFormatter.setMaximum(3600);
sleepFormatter.setAllowsInvalid(false);
sleepFormatter.setCommitsOnValidEdit(true);// committ value on each keystroke instead of focus lost
JFormattedTextField textFieldSleep = new JFormattedTextField(sleepFormatter);
textFieldSleep.setText("0");