下面列出了javax.swing.table.TableColumnModel#moveColumn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void apply(TableColumnModel tcm) {
int count = Math.min(tcm.getColumnCount(), widths.length);
for(int i = 0; i < count; i++) {
TableColumn col = tcm.getColumn(i);
col.setPreferredWidth(widths[col.getModelIndex()]);
col.setWidth(widths[col.getModelIndex()]);
}
int last = Math.min(tcm.getColumnCount(), order.length) - 1;
int idx = 0;
for(int i = last; i >= 0; i--) {
for(int j = 0; j <= i; j++) {
if(tcm.getColumn(j).getModelIndex() == order[idx]) {
tcm.moveColumn(j, last);
break;
}
}
idx++;
}
installListeners(tcm);
}
@Override
public boolean restoreColumnsConfig() {
Preconditions.checkState(table != null, "instance was detached, can't do much after that");
ArrayList<Pair<Integer, Integer>> cc = configPairs.find(keyId, null);
if (cc == null) {
return false;
}
TableColumnModel cm = table.getColumnModel();
for (int i = 0; i < cm.getColumnCount(); i++) {
int desiredColumnModelIndex = cc.get(i).getLeft();
if (cm.getColumn(i).getModelIndex() == desiredColumnModelIndex) {
continue;
}
int desiredColumnPhysicalindex = getColumnPhysicalindexByModelIndex(cm, desiredColumnModelIndex);
cm.moveColumn(desiredColumnPhysicalindex, i);
}
// xet sizes
for (int i = 0; i < cm.getColumnCount(); i++) {
TableColumn c = cm.getColumn(i);
c.setPreferredWidth(cc.get(i).getRight());
}
prevColumnsConfig = cc;
return true;
}
public static void restore(final Storage storage, final JTable table) {
final TableColumnModel columnModel = table.getTableHeader().getColumnModel();
int index = 0;
final ArrayList<String> columnIndices = new ArrayList<String>();
while (true) {
final String order = storage.get(orderPropertyName(index));
if (order == null) break;
columnIndices.add(order);
index++;
if (index == table.getColumnCount()) break;
}
index = 0;
for (final String columnIndex : columnIndices) {
final int modelColumnIndex = indexbyModelIndex(columnModel, Integer.parseInt(columnIndex));
if (modelColumnIndex > 0 && modelColumnIndex < columnModel.getColumnCount()) {
columnModel.moveColumn(modelColumnIndex, index);
}
index++;
}
for (int i = 0; i < columnIndices.size(); i++) {
final String width = storage.get(widthPropertyName(i));
if (width != null && width.length() > 0) {
try {
columnModel.getColumn(i).setPreferredWidth(Integer.parseInt(width));
} catch(NumberFormatException e) {
LOG.error("Bad width: " + width + " at column: "+ i + " from: " + storage +
" actual columns count: " + columnModel.getColumnCount() +
" info count: " + columnIndices.size(), e);
}
}
}
}
private void setColumnsOrder() {
logger.fine("setColumnsOrder()");
TableColumnModel tcm = treeTable.getTable().getColumnModel();
//int[] shift = new int[columns.length];
int defaultColumnVisibleIndex = 0;
for (int i = 0; i < defaultColumnIndex; i++) {
if (!columns[i].isHidden()) {
defaultColumnVisibleIndex++;
}
}
if (defaultColumnVisibleIndex != 0 && defaultColumnVisibleIndex < tcm.getColumnCount()) {
logger.log(Level.FINE, " move default column({0}, {1})", new Object[]{0, defaultColumnVisibleIndex});
tcm.moveColumn(0, defaultColumnVisibleIndex);
}
int n = tcm.getColumnCount();
int[] order = new int[n];
int ci = 0;
for (int i = 0; i < n; i++, ci++) {
while (ci < columns.length && columns[ci].isHidden()) {
ci++;
}
if (ci >= columns.length) {
break;
}
order[i] = columnVisibleMap[ci];
logger.log(Level.FINE, " order[{0}] = {1}", new Object[]{i, order[i]});
}
for (int i = 0; i < n; i++) {
int j = 0;
for (; j < n; j++) {
if (order[j] == i) {
break;
}
}
if (j == n) {
// No "j" for order[j] == i.
continue;
}
logger.log(Level.FINE, " order[{0}] = {1}", new Object[]{j, i});
if (j != i) {
for (int k = j; k > i; k--) {
order[k] = order[k-1];
}
order[i] = i;
logger.log(Level.FINE, " move column({0}, {1})", new Object[]{j, i});
tcm.moveColumn(j, i);
}
}
}