Java Swing How to - Remove the first visible column without removing the underlying data








Question

We would like to know how to remove the first visible column without removing the underlying data.

Answer

// ww  w .  j a v  a  2 s .  co  m
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new MyDefaultTableModel();
    JTable table = new JTable(model);
    table.setModel(model);

    model.addColumn("Col1");
    model.addColumn("Col2");
    model.addColumn("Col3");
    model.addRow(new Object[] { "v1" });


    table.removeColumn(table.getColumnModel().getColumn(0));

    }
}

class MyDefaultTableModel extends DefaultTableModel {
  public Vector getColumnIdentifiers() {
    return columnIdentifiers;
  }
}