L.Banfi L.Banfi - 7 months ago 28
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);

apartma.getColumnModel().getColumn(1).setHeaderValue("newHeader");


Header not showing up

Answer

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);
yourPanel.add(sc);

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.

Comments