JDK-4776303 : Changing JTableHeader after a JTable has been created will not show the header
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: x86
  • Submitted: 2002-11-08
  • Updated: 2023-07-27
  • Resolved: 2023-07-27
Related Reports
Duplicate :  
Relates :  
Description
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);
    }
}

Comments
EVALUATION See also 6521138.
12-02-2007

EVALUATION Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=12156&forumID=1463
21-03-2006

WORK AROUND Workaround: Call the JTableHeader(TableColumnModel) constructor before setting the JTableHeader on the JTable.
25-09-2004

SUGGESTED FIX JTable.setTableHeader should check for the existence of columnModel and set it on the new JTableHeader. I suppose the columModel on the old JTableHeader should be cleared - for the reasons that the JTable reference was also cleared.
25-09-2004