问题描述
我在我的应用程序中为JTable编写了AbstractTableModel.从教程中可以看出,我可以通过获取列模型,然后获取特定的列,使该列具有组合框,例如:
I have a written an AbstractTableModel for my JTable in my application. I see from tutorials that I can make the column to have a combobox by getting the column model and then the specific column for example:
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));
但是如何针对特定的单元格或行执行此操作?
But how do I do this for a specific cell or a row?
推荐答案
JTable的默认实现是基于列的.如果要具有基于行或单单元格的选择,则更改该行为的唯一方法是创建JTable
的子类并覆盖方法public TableCellEditor getCellEditor(int row, int column)
.在实现内部,您可以使用提供的行索引和列索引做出不同的选择. JTable将始终使用此方法来获取单元格编辑器.
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.
这篇关于使用AbstractTableModel更改特定单元格的单元格编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!