e922. Removing a Column from a JTable Component
To remove a column to a JTable component, the component must use a table model that supports this operation. A simple implementation of such a table model is DefaultTableModel. 
DefaultTableModel.removeColumn() removes the visible column, but leaves the column data in the table model. This example provides a routine that removes both the visible column and the column data.     DefaultTableModel model = new MyDefaultTableModel();
    JTable table = new JTable(model);
    table.setModel(model);
    
    // Create 3 columns
    model.addColumn("Col1");
    model.addColumn("Col2");
    model.addColumn("Col3");
    model.addRow(new Object[]{"v1"});
    
    // Remove the first visible column without removing the underlying data
    table.removeColumn(table.getColumnModel().getColumn(0));
    
    // Disable autoCreateColumnsFromModel to prevent
    // the reappearance of columns that have been removed but
    // whose data is still in the table model
    table.setAutoCreateColumnsFromModel(false);
    
    // Remove the first visible column and its data
    removeColumnAndData(table, 0);
    
    // Remove the last visible column and its data
    removeColumnAndData(table, table.getColumnCount()-1);
    
    // Removes the specified column from the table and the associated
    // call data from the table model.
    public void removeColumnAndData(JTable table, int vColIndex) {
        MyDefaultTableModel model = (MyDefaultTableModel)table.getModel();
        TableColumn col = table.getColumnModel().getColumn(vColIndex);
        int columnModelIndex = col.getModelIndex();
        Vector data = model.getDataVector();
        Vector colIds = model.getColumnIdentifiers();
    
        // Remove the column from the table
        table.removeColumn(col);
    
        // Remove the column header from the table model
        colIds.removeElementAt(columnModelIndex);
    
        // Remove the column data
        for (int r=0; r<data.size(); r++) {
            Vector row = (Vector)data.get(r);
            row.removeElementAt(columnModelIndex);
        }
        model.setDataVector(data, colIds);
    
        // Correct the model indices in the TableColumn objects
        // by decrementing those indices that follow the deleted column
        Enumeration enum = table.getColumnModel().getColumns();
        for (; enum.hasMoreElements(); ) {
            TableColumn c = (TableColumn)enum.nextElement();
            if (c.getModelIndex() >= columnModelIndex) {
                c.setModelIndex(c.getModelIndex()-1);
            }
        }
        model.fireTableStructureChanged();
    }
    
    // This subclass adds a method to retrieve the columnIdentifiers
    // which is needed to implement the removal of
    // column data from the table model
    class MyDefaultTableModel extends DefaultTableModel {
        public Vector getColumnIdentifiers() {
            return columnIdentifiers;
        }
    }

解决方案 »

  1.   

    DefaultTableModel里面用了一个vector来保存列,你对这个vector进行处理就可以控制了
      

  2.   

    http://javaalmanac.com/egs/javax.swing.table/InsertCol.html
    e921. Inserting a Column in a JTable Component
    There is no insertColumn method like DefaultTableModel.insertRow() for inserting rows. In order to insert a column at a particular position, you must append the column using DefaultTable.addColumn() and then move the new column into the desired position. See e920 Appending a Column to a JTable Component and e923 Moving a Column in a JTable Component for more information. 
        int rows = 10;
        int cols = 5;
        JTable table = new JTable(rows, cols);
        
        // Disable autoCreateColumnsFromModel
        table.setAutoCreateColumnsFromModel(false);
        
        // Add data
        
        // Insert a new column at position 2 (becomes 3rd column)
        insertColumn(table, "New Column", null, 2);
        
        // Insert a new column at the end
        insertColumn(table, "New Column", null, table.getColumnCount());
        
        // Creates a new column at position vColIndex
        public void insertColumn(JTable table, Object headerLabel,
                                 Object[] values, int vColIndex) {
            // This method is defined in
            // e920 Appending a Column to a JTable Component
            betterAddColumn(table, headerLabel, values);
        
            table.moveColumn(table.getColumnCount()-1, vColIndex);
        }e920. Appending a Column to a JTable Component
    To add a column to a JTable component, the component must use a table model that supports this operation. A simple implementation of such a table model is DefaultTableModel. 
    The simplest way to add a column to a JTable component using a DefaultTableModel table model is to call DefaultTableModel.addColumn(). You need only supply the name and data for the new column and a new visible column will appear in the table. However, this method of column creation is not suitable after existing columns have undergone customizations or after the user has made adjustments to the existing columns. All customizations and adjustments are lost after the new column is added. For example, if you've installed a special renderer on one of the columns or if the user has moved a column, all these changes will be lost when the new column is added. In fact, if you remove a column (but did not remove the column data), the column will reappear after the new column is added. This example provides a routine that will add a column without affecting the state of the existing columns. In order for the routine to work, the autoCreateColumnsFromModel property must be set to false. This property causes the table component to rebuild all the columns when a column is added to the table model. When set to false, the table component will no longer add a visible column when a column is added to the table model. This step must now be done explicitly.     DefaultTableModel model = new DefaultTableModel();
        JTable table = new JTable(model);
        
        // Add a column using the simple method
        model.addColumn("Col1");
        
        // Add a column with values.
        // The list of values are appended to the end of the
        // existing rows. However, if there are more column values
        // than there are rows, new rows with null values are
        // created to accommodate the extra column values.
        model.addColumn("Col2", new Object[]{"v2"});
        // there is now 1 row with 2 columns
        
        // Disable autoCreateColumnsFromModel
        table.setAutoCreateColumnsFromModel(false);
        
        // Add a column without affecting existing columns
        betterAddColumn(table, "Col3", new Object[]{"v3"});
        
        // This method adds a new column to table without reconstructing
        // all the other columns.
        public void betterAddColumn(JTable table, Object headerLabel,
                                    Object[] values) {
            DefaultTableModel model = (DefaultTableModel)table.getModel();
            TableColumn col = new TableColumn(model.getColumnCount());
        
            // Ensure that auto-create is off
            if (table.getAutoCreateColumnsFromModel()) {
                throw new IllegalStateException();
            }
            col.setHeaderValue(headerLabel);
            table.addColumn(col);
            model.addColumn(headerLabel.toString(), values);
        }