所以我使用 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 ;
}
添加回答
举报
0/150
提交
取消