为了账号安全,请及时绑定邮箱和手机立即绑定

在 JTable 中添加 JSpinner 时出现非法参数异常

在 JTable 中添加 JSpinner 时出现非法参数异常

三国纷争 2023-02-16 16:19:54
所以我使用 AbstractCellEditor 和 TableCellEditor 类在 Jtable 的单元格内添加了一个 JSpinner mt SpinnerEditor 类非常简单,代码如下:public class SpinnerEditor extends AbstractCellEditor implements TableCellEditor {    final JSpinner spinner;    SpinnerEditor(){        spinner = new JSpinner();    }    @Override    public Object getCellEditorValue() {        return spinner.getValue();    }    @Override    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {        spinner.setValue(value);        return spinner ;    }    @Override    public boolean isCellEditable(EventObject evt){    return true;    }}问题是当我尝试通过单击它来编辑单元格时,我得到一个非法参数异常:Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal value    at javax.swing.SpinnerNumberModel.setValue(SpinnerNumberModel.java:443)    at javax.swing.JSpinner.setValue(JSpinner.java:354)    at timetablemgmt.SpinnerEditor.getTableCellEditorComponent(SpinnerEditor.java:39)    at javax.swing.JTable.prepareEditor(JTable.java:5778)    at javax.swing.JTable.editCellAt(JTable.java:3512)    at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1108)    at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1038)    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)    at java.awt.Component.processMouseEvent(Component.java:6530)    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)    at java.awt.Component.processEvent(Component.java:6298)    at java.awt.Container.processEvent(Container.java:2237)    at java.awt.Component.dispatchEventImpl(Component.java:4889)    at java.awt.Container.dispatchEventImpl(Container.java:2295)    at java.awt.Component.dispatchEvent(Component.java:4711)    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4523)
查看完整描述

1 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

你应该试试:


public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

        spinner.setValue(value);

        return spinner ;

    }



public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

        if(value == null)

           spinner.setValue(0);

        else

           spinner.setValue(value);

        return spinner ;

    }


查看完整回答
反对 回复 2023-02-16
  • 1 回答
  • 0 关注
  • 79 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信