简体   繁体   中英

How to retrieve JFormattedTextField integer value?

The problem is JFormattedTextField automatically changes the Integer to the formatted Integer (Eg: 5000000 to 5,000,000). With JTextField, I can convert 5000000 to Integer using parseInt(). But how can I do that with JFormattedTextField when it's value is 5,000,000.

Use one of the Format classes in java.text:

String num = "5,000";
DecimalFormat format = new DecimalFormat("#,##0.###");
try {
    System.out.println(format.parse(num).toString()); // prints 5000
} catch (ParseException ex) {
    ...
}

I'm watching Java API for JFormattedTextField and I noted that it has a setFormattedFactory(...) method and I think you can use this method to change the format of retrieved values.

Try to follow the answer to this question to use that method : How to change the format of a JFormattedTextField at runtime?

Here instead it's explained how parse a big number with comma : Convert a String to Double - Java

I hope it's useful for you! bye

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM