Question about JTextField and text format with numbers?

こ雲淡風輕ζ 提交于 2019-12-23 18:44:42

问题


I writing an application using Swing. My application has a text box and what I want to do is this: when user enters a number in that text field, if the number is in the thousands, then it is automatically appended a comma in the number. For example, if user enters 1000, then it should be automatically changed to 1,000, or 1,000,000 or so.

Does anyone give me some ideas, please?


回答1:


You can use the DecimalFormat and NumberFormat classes. See an example here; http://www.exampledepot.com/egs/java.text/FormatNum.html

Update:

Example:

NumberFormat numberFormatter = new DecimalFormat("#,###,###");
String formattedNumber = numberFormatter.format(10000000);

JTextField numberTextField = new JTextField();

numberTextField.setText(formattedNumber);



回答2:


Try formattedTextFields

Also one more example. May be helpful to you.




回答3:


Have a look at group seperators in DecimalFormat .



来源:https://stackoverflow.com/questions/5498109/question-about-jtextfield-and-text-format-with-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!