Jtabe更新数据后,table保持列宽
public class TableSaveAutoColumnSize {
?
? ? List<TableColumn> tableColumnList = new ArrayList<TableColumn>();
? ? JTable table;
?
? ? public TableSaveAutoColumnSize(JTable table) {
? ? ? ? this.table = table;
? ? ? ? saveAutoColumnSize();
? ? }
?
? ? public void saveAutoColumnSize() {
? ? ? ? TableColumnModel tcm = table.getColumnModel();
? ? ? ? Enumeration<TableColumn> cm = tcm.getColumns();
? ? ? ? while (cm.hasMoreElements()) {
? ? ? ? ? ? TableColumn tt = cm.nextElement();
? ? ? ? ? ? tableColumnList.add(tt);
? ? ? ? }
? ? }
?
? ? public void resetWithAutoColumnSize() {
? ? ? ? TableColumnModel tcm = table.getColumnModel();
? ? ? ? Enumeration<TableColumn> cm = tcm.getColumns();
? ? ? ? int j = 0;
? ? ? ? while (cm.hasMoreElements()) {
? ? ? ? ? ? TableColumn tt = cm.nextElement();
? ? ? ? ? ? tt.setWidth(tableColumnList.get(j).getWidth());
? ? ? ? ? ? tt.setPreferredWidth(tableColumnList.get(j).getPreferredWidth());
? ? ? ? ? ? j++;
? ? ? ? }
? ? }
}
?
使用方式:
? ? ? ? ? ? TableSaveAutoColumnSize ts = new TableSaveAutoColumnSize(table);
? ? ? ? ? ? model.setDataVector(data, title);
? ? ? ? ? ? ts.resetWithAutoColumnSize();
?
?