Java Code Examples for javax.swing.table.TableColumnModel#moveColumn()

The following examples show how to use javax.swing.table.TableColumnModel#moveColumn() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Preferences.java    From pdfxtk with Apache License 2.0 6 votes vote down vote up
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);
   }
 
Example 2
Source File: TableColumnsGeometryPersisterImpl.java    From pgptool with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 3
Source File: BaseTableView.java    From consulo with Apache License 2.0 5 votes vote down vote up
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);
      }
    }
  }
}
 
Example 4
Source File: OutlineTable.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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);
        }
    }
}