• Advertisement
Sign in to follow this  

[java] JTable headers?

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heya, I'm playing around with swing JTables, but can't for the life of me get them to show a header. At the moment, what I'm doing is calling the constructor which takes (data[][],rowheaders[]), then adding the table directly to my JFrame's content pane, then calling setVisible on the object. However, I only get the table itself to show, rather than the header too. I've played around with adding the header object, but to no avail - I still only get the actual table, no headers. Can anyone point me in the right direction as to what else I need to call? CJM

Share this post


Link to post
Share on other sites
Advertisement
To be able to show the header, you need to add the table to a JScrollPane. The scroll pane automatically gets the table's header, and puts it on top of the table. For example you do something like this:

JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(200,200));

If you are not using JScrollPanes, you need to explicity add the table header yourself like this:

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

That should solve your problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement