JTable with JComboBox for DB Foreign Key in Swing: Getting/Setting 值

JTable with JComboBox for DB Foreign Key in Swing: Getting/Setting value

这是一个 JComboBox 的示例,其中包含带有 ID 和描述的自定义对象。 JCombobox string item (visible) and integer key (inherent)

我必须(通常)填充各种此类 JComboBox 对象,以便在各种 JTable 实例中为外键呈现值。

我在 JTable 中继承 DefaultTableModel。我们会说我按照上面的示例并为外键字段实现了 Vector of Item(ID 可能与列表索引不对应)。 JComboBox 选择索引是如何或在哪里设置的?

How or where does the JComboBox selection index get set?

您需要实现项目 class 的 equals(...) 方法。

@Override
public boolean equals(Object object)
{
    Item item = (Item)object;
    return value.equals(item.getValue());
}

以上代码取自 Combo Box With Hidden Data,其中包含项目 class 的更完整实现。

然后将项目对象存储在 TableModel 中。当您编辑单元格时,组合框中的项目将被选中。