我來到另一個解決方案,因爲我想重用默認編輯器...下面的類重新定義getColumnClass以得到不同的答案。據我測試,它工作正常,我可以使用setDefaultEditor等。您可以注意到,可以增強此行爲以將其僅應用於期望的列。
public class JXMultiTypeColumnTable extends JXTable {
private Map<Integer, Class<?>> viewedClassByColumn = new HashMap<Integer, Class<?>>();
public JXMultiTypeColumnTable(Object[][] rowData, Object[] columnNames) {
super(rowData, columnNames);
}
public JXMultiTypeColumnTable(int numRows, int numColumns) {
super(numRows, numColumns);
}
public JXMultiTypeColumnTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
super(dm, cm, sm);
}
public JXMultiTypeColumnTable(TableModel dm, TableColumnModel cm) {
super(dm, cm);
}
public JXMultiTypeColumnTable(TableModel dm) {
super(dm);
}
public JXMultiTypeColumnTable() {
}
@Override
public Class<?> getColumnClass(int column) {
Class<?> recordedClass = this.viewedClassByColumn.get(column);
if (recordedClass != null) {
return recordedClass;
}
return super.getColumnClass(column);
}
private void recordViewedClass(int row, int column) {
this.viewedClassByColumn.put(column,
this.getModel().getValueAt(
this.convertRowIndexToModel(row), this.convertColumnIndexToModel(column))
.getClass());
}
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
this.recordViewedClass(row, column);
return super.getCellRenderer(row, column);
}
@Override
public TableCellEditor getCellEditor(int row, int column) {
this.recordViewedClass(row, column);
return super.getCellEditor(row, column);
}
}
注:可以擴展JTable
而不是JXTable
。