下面列出了javax.swing.table.TableColumnModel#removeColumn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void createDefaultColumnsFromModel() {
TableModel m = getModel();
if( m != null ) {
// Remove any current columns
TableColumnModel cm = getColumnModel();
while( cm.getColumnCount() > 0 ) {
cm.removeColumn( cm.getColumn(0) );
}
// Create new columns from the data model info
for( int i=0; i<m.getColumnCount(); i++ ) {
TableColumn newColumn = new MyTableColumn(i);
if( i == TaskListModel.COL_LOCATION )
newColumn.setCellRenderer( new LeftDotRenderer() );
else if( i != TaskListModel.COL_GROUP )
newColumn.setCellRenderer( new TooltipRenderer() );
addColumn(newColumn);
}
}
}
/**
* deletes all selected columns if it is not present in the <code>exp</code>
* List
*
* @param table the table to DELETE columns
* @param exp columns to avoid deleting
* @see #deletecol(javax.swing.JTable, int)
*/
static void deletecols(JTable table, int[] exp) {
Integer[] selcols;
try {
TableColumnModel tcm = table.getColumnModel();
selcols = ArrayUtils.toObject(table.getSelectedColumns());
Arrays.sort(selcols, Collections.reverseOrder());
List<Integer> explist = Ints.asList(exp);
for (int i : selcols) {
if (!explist.contains(i)) {
tcm.removeColumn(tcm.getColumn(i));
}
}
} catch (Exception e) {
Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, e);
}
}
protected void recreateTableColumn(TableColumnModel columnModel) {
int n = columnModel.getColumnCount();
EditableTableHeaderColumn[] newCols = new EditableTableHeaderColumn[n];
TableColumn[] oldCols = new TableColumn[n];
for (int i = 0; i < n; i++) {
oldCols[i] = columnModel.getColumn(i);
newCols[i] = new EditableTableHeaderColumn(i);
newCols[i].copyValues(oldCols[i]);
}
for (int i = 0; i < n; i++) {
columnModel.removeColumn(oldCols[i]);
}
for (int i = 0; i < n; i++) {
columnModel.addColumn(newCols[i]);
}
}
public void setView(List columns) {
TableColumnModel model = getColumnModel();
// Remove all the columns:
for (int f = 0; f < _numCols; f++) {
model.removeColumn(_tableColumns[f]);
}
Iterator selectedColumns = columns.iterator();
Vector columnNameAndNumber = getColumnNameAndNumber();
while (selectedColumns.hasNext()) {
// add the column to the view
model.addColumn(_tableColumns[columnNameAndNumber.indexOf(selectedColumns.next())]);
}
//SWING BUG:
sizeColumnsToFit(-1);
}
public final void setView(final List columns) {
final TableColumnModel model = getColumnModel();
// Remove all the columns:
for (int f = 0; f < _numCols; f++) {
model.removeColumn(_tableColumns[f]);
}
final Iterator selectedColumns = columns.iterator();
final Vector columnNameAndNumber = getColumnNameAndNumber();
while (selectedColumns.hasNext()) {
// add the column to the view
model.addColumn(_tableColumns[columnNameAndNumber.indexOf(selectedColumns.next())]);
}
//SWING BUG:
sizeColumnsToFit(-1);
}
/**
* Returns an {@link java.util.Iterator} which traverses the TableColumnModel one TableColumn at a time.
*
* @param self a TableColumnModel
* @return an Iterator for a TableColumnModel
* @since 1.6.4
*/
public static Iterator<TableColumn> iterator(final TableColumnModel self) {
return new Iterator<TableColumn>() {
private int index = 0;
public boolean hasNext() {
return index > -1 && index < self.getColumnCount();
}
public TableColumn next() {
return self.getColumn(index++);
}
public void remove() {
if (hasNext()) self.removeColumn(self.getColumn(index--));
}
};
}
/**
* Creates default columns for the table from the data model using the <code>getColumnCount</code> method defined in
* the <code>TableModel</code> interface.
* <p/>
* Clears any existing columns before creating the new columns based on information from the model.
*
* @see #getAutoCreateColumnsFromModel
*/
public void createDefaultColumnsFromModel() {
final TableModel m = getModel();
if ( m != null ) {
// Remove any current columns
final TableColumnModel cm = getColumnModel();
while ( cm.getColumnCount() > 0 ) {
cm.removeColumn( cm.getColumn( 0 ) );
}
// Create new columns from the data model info
for ( int i = 0; i < m.getColumnCount(); i++ ) {
if ( i == 0 ) {
final TableColumn column = new TableColumn( i );
column.setCellRenderer( tableHeader.getDefaultRenderer() );
addColumn( column );
continue;
}
final EditableHeaderTableColumn newColumn = new EditableHeaderTableColumn( i );
newColumn.setHeaderEditor( new TypedHeaderCellEditor() );
addColumn( newColumn );
}
}
}
protected void recreateTableColumn(TableColumnModel columnModel) {
int n = columnModel.getColumnCount();
EditableHeaderTableColumn[] newCols = new EditableHeaderTableColumn[n];
TableColumn[] oldCols = new TableColumn[n];
for (int i=0;i<n;i++) {
oldCols[i] = columnModel.getColumn(i);
newCols[i] = new EditableHeaderTableColumn();
newCols[i].copyValues(oldCols[i]);
}
for (int i=0;i<n;i++) {
columnModel.removeColumn(oldCols[i]);
}
for (int i=0;i<n;i++) {
columnModel.addColumn(newCols[i]);
}
}
private static void removeColumns(final JTable table) {
final TableColumnModel tcm = table.getColumnModel();
while (tcm.getColumnCount() > 0) {
final TableColumn tc = tcm.getColumn(0);
tcm.removeColumn(tc);
}
}
public void testRemoveNodeColumn() throws InterruptedException, IllegalAccessException, InvocationTargetException {
final TableColumnModel model = view.getOutline().getColumnModel();
model.removeColumn(model.getColumn(0));
assertEquals("One column visible", 1, model.getColumnCount());
component.addNotify();
final Node n0 = rootNode.getChildren().getNodeAt(0);
rootNode.getChildren().remove(new Node[] { n0});
assertEquals("One column visible after remove", 1, model.getColumnCount());
rootNode.getChildren().add(new Node[] { n0});
assertEquals("One column visible after add", 1, model.getColumnCount());
}
public static void getJTableOptions(final String name, final JTable table,
final Properties properties) {
final Integer colCount = getObjectInteger(name + "_col_count",
properties);
if (colCount == null || colCount.intValue() != table.getColumnCount())
return;
final String cNames[] = new String[table.getColumnCount()];
final Object cols[] = new Object[table.getColumnCount()];
for (int i = 0; i < cNames.length; i++) {
cNames[i] = table.getColumnName(i);
cols[i] = table.getColumnModel().getColumn(i);
}
for (final String element : cNames) {
final int width = getInteger(name + "_col_" + element + "_width",
table.getColumn(element).getWidth(), properties);
table.getColumn(element).setPreferredWidth(width);
}
final TableColumnModel cm = table.getColumnModel();
final int tci[] = new int[cNames.length];
for (int i = 0; i < cNames.length; i++)
cm.removeColumn((TableColumn) cols[i]);
for (int i = 0; i < cNames.length; i++) {
tci[i] = getInteger(name + "_col_" + cNames[i] + "_index", i,
properties);
}
for (int i = 0; i < cNames.length; i++)
for (int j = 0; j < cNames.length; j++)
if (tci[j] == i)
cm.addColumn((TableColumn) cols[j]);
}
public void setDetailedView() {
//TODO: Defineable Views.
TableColumnModel model = getColumnModel();
// Remove all the columns:
for (int f = 0; f < _numCols; f++) {
model.removeColumn(_tableColumns[f]);
}
// Add them back in the correct order:
for (int i = 0; i < _numCols; i++) {
model.addColumn(_tableColumns[i]);
}
//SWING BUG:
sizeColumnsToFit(-1);
}
public final void setDetailedView() {
//TODO: Definable Views.
final TableColumnModel model = getColumnModel();
// Remove all the columns:
for (int f = 0; f < _numCols; f++) {
model.removeColumn(_tableColumns[f]);
}
// Add them back in the correct order:
for (int i = 0; i < _numCols; i++) {
model.addColumn(_tableColumns[i]);
}
//SWING BUG:
sizeColumnsToFit(-1);
}
public static void getJTableOptions(final String name, final JTable table,
final Properties properties) {
final Integer colCount = getObjectInteger(name + "_col_count",
properties);
if (colCount == null || colCount.intValue() != table.getColumnCount())
return;
final Object cols[] = new Object[table.getColumnCount()];
for (int i = 0; i < colCount.intValue(); i++) {
cols[i] = table.getColumnModel().getColumn(
table.convertColumnIndexToView(i));
}
for (int i = 0; i < colCount.intValue(); i++) {
try {
int index = table.convertColumnIndexToView(i);
final int width = getInteger(name + "_col_" + i + "_width",
table.getColumnModel().getColumn(index).getWidth(),
properties);
table.getColumnModel().getColumn(index)
.setPreferredWidth(width);
} catch (Exception e) {
}
}
final TableColumnModel cm = table.getColumnModel();
final int tci[] = new int[colCount.intValue()];
for (int i = 0; i < colCount.intValue(); i++)
cm.removeColumn((TableColumn) cols[i]);
for (int i = 0; i < colCount.intValue(); i++) {
tci[i] = getInteger(name + "_col_" + i + "_index", i, properties);
}
for (int i = 0; i < colCount.intValue(); i++)
for (int j = 0; j < colCount.intValue(); j++)
if (tci[j] == i)
cm.addColumn((TableColumn) cols[j]);
}