With below example code:
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.
With below example code: 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.
Right-click the table and select "Properties." The table's properties display, including the columns. You click a column and change the heading to edit the current columns. To add a new column, click "New" and type a heading for the column.
Class DefaultTableModel. This is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects. Warning: DefaultTableModel returns a column class of Object .
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With