时间:2010-07-26 10:54:18

标签: java swing jtable

2 个答案:

答案 0 :(得分:8)

答案 1 :(得分:1)

我来到另一个解决方案,因为我想重用默认编辑器......以下类重新定义了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);
}

}

N.B。可以扩展JTable而不是JXTable