fetching float values from jformatted text field
I am able to create JFormatted TextField that accepts only float values,but I am not able to fetch that value.... I am declaring it ..
stopAppFormattedTextField = new javax.swing.JFormattedTextField(new DecimalFormat("#.00"));
and fetching the value using :
doubl开发者_高级运维e stop=(Double)stopAppFormattedTextField.getValue();
but the above statement is throwing the following exception:
"Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double"
what should I do to fetch float values? -Thanks in advance
You're getting Long
s from the formatter, but you want double
s. You can do this:
Number number = (Number)stopAppFormattedTextField.getValue();
double stop = number.doubleValue();
Add this to your code after initialization:
stopAppFormattedTextField.setValue(0d);
The getValue will return double auto-magically
精彩评论