读书人

Jtabe更新数据后table护持列宽

发布时间: 2013-08-01 15:23:18 作者: rapoo

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();

?

?

读书人网 >编程

热点推荐