L.Banfi L.Banfi - 1 year ago 101
Java Question

Determining the header of each column in a JTable

I created a JTable that has 6 rows and 8 columns. I want to set the header for each column. I tried the code bellow and it didn't work for me.

JTable apartma = new JTable(6,8);


Header not showing up

Answer Source

You didn't provide a complete code, but it seems that the problem is the table header is not visible.

In order to show the JTable's header, you should put the JTable in a JScrollPane, and do not add your JTable instance directly to the underlying container:

JScrollPane sc = new JScrollPane(apartma);

Also you may want to pass a String[] or Vector<String> as the titles for all of the JTable columns header, to the TableModel.

Hope this would be helpful.