JTable won't show column headers
Put your JTable
inside a JScrollPane
. Try this:
add(new JScrollPane(scrTbl));
The main difference between this answer and the accepted answer is the use of setViewportView()
instead of add()
.
How to put JTable
in JScrollPane
using Eclipse IDE:
- Create
JScrollPane
container via Design tab. - Stretch
JScrollPane
to desired size (applies to Absolute Layout). - Drag and drop
JTable
component on top ofJScrollPane
(Viewport area).
In Structure > Components, table
should be a child of scrollPane
.
The generated code would be something like this:
JScrollPane scrollPane = new JScrollPane();
...
JTable table = new JTable();
scrollPane.setViewportView(table);
As said in previous answers the 'normal' way is to add it to a JScrollPane, but sometimes you don't want it to scroll (don't ask me when:)). Then you can add the TableHeader yourself. Like this:
JPanel tablePanel = new JPanel(new BorderLayout());
JTable table = new JTable();
tablePanel.add(table, BorderLayout.CENTER);
tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);