Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable with custom column model and table model not showing table header

805419Oct 14 2010 — edited Oct 14 2010
Hello,

I am creating a JTable with a custom table model and a custom column model. However the table header is not being displayed (yes, it is in a JScrollPane). I've shrunk the problem down into a single compileable example:

Thanks for your help.
import javax.swing.*;
import javax.swing.table.*;

public class Test1 extends JFrame
{
	public static void main(String args[])
	{
		JTable table;
		TableColumnModel colModel=createTestColumnModel();
		TestTableModel tableModel=new TestTableModel();
		Test1 frame=new Test1();

		table=new JTable(tableModel, colModel);
		frame.getContentPane().add(new JScrollPane(table));

		frame.setSize(200,200);
		frame.setVisible(true);
	}

	private static DefaultTableColumnModel createTestColumnModel()
	{
		DefaultTableColumnModel columnModel=new DefaultTableColumnModel();
		columnModel.addColumn(new TableColumn(0));

		return columnModel;
	}

	static class TestTableModel extends AbstractTableModel
	{
		public int getColumnCount()
		{
			return 1;
		}

		public Class<?> getColumnClass(int columnIndex)
		{
			return String.class;
		}

		public String getColumnName(int column)
		{
			return "col";
		}

		public int getRowCount()
		{
			return 1;
		}

		public Object getValueAt(int row, int col)
		{
			return "test";
		}

		public void setValueAt(Object aValue, int rowIndex, int columnIndex)
		{
		}
	}
}
Edited by: 802416 on 14-Oct-2010 04:29
added
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
This post has been answered by jduprez on Oct 14 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 11 2010
Added on Oct 14 2010
5 comments
352 views