JSpinner采用双格式验证

时间:2015-01-05 15:54:36

标签: java swing jspinner

我有一个JFrame女巫作为用户输入表单,它包含许多类型为JSpinner的双字段字段。

前:

 Double current = 0.0;
 Double min = (double) Integer.MIN_VALUE;
 Double max = (double) Integer.MAX_VALUE;
 Double step = 0.1;

 JSpinner mySpinnerExample = new JSpinner(new SpinnerNumberModel(current, min, max, step));

当mySpinnerExample为空(默认值为0)并且用户点击O.K按钮时,我收到此错误。

她是完整的StackTrace

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Double
    at VIEW.ProduitUi.getFormData(ProduitUi.java:530)
    at CONTROLLER.ProduitController.getUiFormData(ProduitController.java:124)
    at CONTROLLER.ProduitController.createProduit(ProduitController.java:106)
    at VIEW.ProduitUi$2.actionPerformed(ProduitUi.java:284)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6527)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6292)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4883)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我如何克服这个问题。

1 个答案:

答案 0 :(得分:3)

您的堆栈跟踪表明您的软件期望Double,但获得BigDecimal。

尝试在SpinnerNumberModel的构造中将Double替换为double。我怀疑java使用了这个构造函数

SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize)

而不是这个

SpinnerNumberModel(double value, double minimum, double maximum, double stepSize)

(见javadoc

所以你的代码应该是

double current = 0.0;
double min = (double) Integer.MIN_VALUE;
double max = (double) Integer.MAX_VALUE;
double step = 0.1;

JSpinner mySpinnerExample = new JSpinner(new SpinnerNumberModel(current, min, max, step))

"双"是" Number"," BigDecimal"也是,但是" Double"不是" double",虽然两者有些相关并且很容易彼此分配。