Jace Bayless
Jace Bayless

Reputation: 21

JTable not displaying Header

I am creating a GUI using Swing in Java. I created a GridLayout and put this table in a cell of the grid layout.

String[] columnNames = {"First Name", "Last Name"};
String[][] data = { { "Name1", "Price1" }, { "Name2", "Price2" }, { "Name3", "Price3" }, { "Name4", "Price4" } };
JTable botTable = new JTable(data, columnNames);

My table is showing the data in the table, but not the table header. How to show the headers in the table?

Upvotes: 1

Views: 49

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

It's likely you've not wrapped your table in a JScrollPane before having the JScrollPane added to something that is displayed on the screen.

See How to Use Tables and How to Use Scroll Panes for more details

JTable botTable = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(botTable);

add(scrollPane);

Upvotes: 4

Related Questions