我有一个JTable
实现AbstractTableModel
. 模型包含自定义对象,这些对象在其字段之一中RequisitionItem
也有一个对象。Section
向表中插入新记录时,我添加了一个新行,其中新行RequisitionItem
初始化为非空但为空值。对于Section
列,我有表格和组合框的自定义渲染器,如下所示
对于桌子;
requestItemsTable.getColumnModel().getColumn(3).setCellRenderer(new DefaultTableCellRenderer(){
public void setValue(Object value) {
if (value==null) {
setText("");
} else {
Section section = (Section) value;
setText(section.getName());
}
}});
对于组合框;
sectionComboBox.setRenderer(new BasicComboBoxRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null) {
setText(((Section) value).getName());
}
if (index == -1) {
setText("");
}
return this;
}
});
对于编辑,我有以下内容;
sectionComboBox = new JComboBox<>();
sectionComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(sectionJpaController.getDepartmentSections(department.getNumber()).toArray(new Section[0])));
requestItemsTable.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(sectionComboBox));
但是在单击Section
单元格后,选择Section
组合框中的一项并按 Enter 键后,我得到了java.lang.ClassCastException: java.lang.String cannot be cast to ***.model.domain.Section
. 那么为什么返回的DefaultCellEditor
不是section对象而是String呢?