1

我在我的应用程序中为我的 JTable 编写了一个 AbstractTableModel。我从教程中看到,我可以通过获取列模型然后获取特定列来使列具有组合框,例如:

TableColumn sportColumn = table.getColumnModel().getColumn(2);
...
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

但是如何为特定的单元格或行执行此操作?

4

2 回答 2

3

The JTable’s default implementation is column-based. The only way to change that behavior, if you want to have row or single-cell based choices, is to create a subclass of JTable and override the method public TableCellEditor getCellEditor(int row, int column). Inside your implementation you can use the provided row and column indices to make a different choice. The JTable will always use this method to get the cell editor.

于 2013-09-11T08:38:50.883 回答
2

你需要使用,覆盖

  1. 准备编辑

  2. TableCellEditor(需要同步编辑器和渲染器)

于 2013-09-11T08:42:46.960 回答