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

所以我使用 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)



三国纷争
浏览 80回答 1
1回答

慕村225694

你应该试试: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 ;    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java