开发者

About auto formatting numbers in java swing

开发者 https://www.devze.com 2023-04-03 12:12 出处:网络
Now I need to make an applet and make use of JTextField or JFormattedTextField which is for user to input numbers. I wo开发者_JS百科uld like to ask how to automatically format the input number dynamic

Now I need to make an applet and make use of JTextField or JFormattedTextField which is for user to input numbers. I wo开发者_JS百科uld like to ask how to automatically format the input number dynamically during user input just like what normal calculator do?

For example: When the user enters 1000, the display in JTextField/JFormattedTextField will be 1,000 and when the user continue to input one zero digit, then the display will be 10,000

Thank you.


If you want the comma to be placed/removed while the user is still editing the JTextField you can't use a PropertyChangedListener as the event is only triggered when the field loses focus or the user hits the enter key (from @trashgod's link).

If you use a DocumentListener instead, you can capture insertUpdate and removeUpdate events which happen as the user is typing. When you capture these events, write some code that will start at the first digit to the left of the decimal point and remove/insert commas as necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消