When JTable.setTableHeader(JTableHeader) is called, the resulting table will not display the new header. The problem is that the JTableHeader requires a TableColumModel to get the column data and its not set anywhere in the call or event chain.
There could be a potential memory leak as the reference to the previous column model is not freed when the previous JTableHeader is changed.
>>>>>>>> TableHeaderBug
import javax.swing.*;
import javax.swing.table.*;
public class TableHeaderBug extends JFrame {
public TableHeaderBug() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(new AbstractTableModel() {
public int getRowCount() { return 10; }
public int getColumnCount() { return 10; }
public Object getValueAt(int row, int column) {
return new Integer(row * column);
}
});
// Enabling this line doens't produce a table header.
table.setTableHeader(new JTableHeader());
// workaround is to use the JTableHeader(TableColumnModel) ctor
//table.setTableHeader(new JTableHeader(table.getColumnModel()));
getContentPane().add(new JScrollPane(table));
pack();
}
public static void main(String[] args) {
TableHeaderBug bug = new TableHeaderBug();
bug.setVisible(true);
}
}