简体   繁体   中英

Set a float value to JSpinner with 2 decimals

I need to set a float value of a JSpinner with 2 decimals, but I always have this error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal value
at java.desktop/javax.swing.SpinnerNumberModel.setValue(SpinnerNumberModel.java:456)
at java.desktop/javax.swing.JSpinner.setValue(JSpinner.java:355)

all Spinners has this model --> SpinnerNumberModel(0.01, 0.01, 10000.00, 0.01);

float a = Float.parseFloat(aSpinner.getValue().toString());
float b = Float.parseFloat(bSpinner.getValue().toString());
float c = b / 100 * 95;
DecimalFormat decimalformat = new DecimalFormat("#.##");
cSpinner.setValue(decimalformat.format(c));

What am I doing wrong?

I tried to give it a float value and it doesn't work, I tried to give it a String value and it doesn't work.

Can someone help me, please?

NumberFormat.format(double) returns a String , where as setValue when the spinner has a SpinnerNumberModel will be expecting a numeric value (eg float or double ).

On a wider note, number formats are only intended to be used for output on a GUI (the view) or printing to the console or a file, etc. Internally (in the model etc) the value should be numeric.

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