JTable column header not visible

You can add following code into your code

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setPreferredSize(new Dimension(0, 0));
table.getTableHeader().setDefaultRenderer(renderer);

The API for JTable states:

"Note that if you wish to use a JTable in a standalone view (outside of a JScrollPane) and want the header displayed, you can get it using getTableHeader() and display it separately."

Or just add the table to a scrollpane, and add your scrollpane to the panel...


Here's how to add a header to a JTable without using a JScrollPane:

int rows = 21;
int cols = 3;
JTable table = new JTable(rows, cols);
JTableHeader header = table.getTableHeader();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(header, BorderLayout.NORTH);
panel.add(table, BorderLayout.CENTER);

Tags:

Java

Swing