tags:

views:

47

answers:

3

String column_names[]= {"Serial Number","Medicine Name","Dose","Frequency"}; table_model=new DefaultTableModel(column_names,3); table=new JTable(table_model);

We want to set header with names of columns as in column_names with the above code but it is not working. Header is not visible though table is getting created.

+3  A: 

To be able to see the header, you should put the table in a JScrollPane.

panel.add(new JScrollPane(table));

Or you could specifically add the tableHeader to your panel if you really don't want a scrollpane (but: normally you don't want this behaviour):

panel.add(table.getTableHeader(), BorderLayout.NORTH);
panel.add(table, BorderLayout.CENTER);
Fortega
A: 

Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for a working example. The trick is to add the table to a JScrollPane.

camickr
+1  A: 

See here for more information about JTables and TableModels

JTable Headers only get shown when the Table is in a scroll pane, which is usually what you want to do anyway. If for some reason, you need to show a table without a scroll pane, you can do:

panel.setLayout(new BorderLayout());
panel.add(table, BorderLayout.CENTER);
panel.add(table.getTableHeader(), BorderLayout.NORTH);
Milan Ramaiya