问题
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?
回答1:
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.
回答2:
you would need to use, override
prepareEditor
TableCellEditor(required to synchronize editor and renderer)
来源:https://stackoverflow.com/questions/18736206/change-cell-editor-for-specific-cells-with-abstracttablemodel