0

我有一个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呢?

4

1 回答 1

0

为什么必须使用如此多的自定义逻辑来呈现和选择组合框中的 Section 对象?

如果我理解正确,您正在尝试仅在 JCombobox 上呈现您的 Section 对象的名称,但能够在 JTable 中检索整个 Section 对象。

要实现这一点,您只需要:

  1. 使用用于选择的 Section 对象向量初始化 JComboBox。

  2. 在其 toString 方法中返回 Section 的名称,因为 JComboBox 评估其项目的 toString 以进行渲染。

  3. 设置要在 CellEditor 中为 Section 的 JTable 列使用的组合框(您已完成)

一个运行的例子是:

public class JTableWithComboboxEditor extends JFrame {
class Section {
    String name;
    int value2;

    public Section(String name, int value2) {
        this.name = name;
        this.value2 = value2;
    }

    @Override
    public String toString() {
        return name;
    }
}

private JTable table = new JTable(new DefaultTableModel(new String[]{"Section"}, 2));


public JTableWithComboboxEditor() {
    this.add(new JScrollPane(table));

    Section section1 = new Section("A", 1);
    Section section2 = new Section("B", 2);
    Vector sectionList = new Vector();
    sectionList.add(section1);
    sectionList.add(section2);

    JComboBox comboBox = new JComboBox(sectionList);

    table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(comboBox));

    table.getModel().addTableModelListener(new TableModelListener() {
        @Override
        public void tableChanged(TableModelEvent e) {
            Object value = table.getValueAt(0, 0);
            if (value != null) {
                Section section = (Section) value;
                System.out.println(section.name + " " + section.value2);
            }
        }
    });
}
}
于 2015-10-11T02:52:26.863 回答