I have a simple application where an array of data passed to a JTable , I specify the names of the columns of the table, but only the data is displayed but not the names of the columns.
Here the code:
public class TableTest extends JFrame
{
private String() columnNames = ("Planet", "Radius", "Moons", "Geseous", "Color");
private Object[][] cells =
{
{"Mercury", 2440.0, 0, false, Color.YELLOW},
{"Venus", 6052 .0, 0, false, Color.YELLOW},
{"Earth", 71492.0, 16, true, Color.BLUE},
{"Mars", 3397.0, 2, false, Color.RED},
{"Jupiter", 71492.0, 16, true, Color.ORANGE},
{"Saturn", 60268.0, 18, true, Color.ORANGE},
{"Uranus", 25559.0, 17, true, Color.BLUE},
{"Neptune", 24766.0, 8, true, Color.BLUE},
{"Pluto", 1137.0, 1, false, Color.BLACK}
};
public TableTest()
{
TableModel model = new DefaultTableModel (cells, columnNames);
JTable table = new JTable (model);
table. setAutoCreateRowSorter (true);
add(table, BorderLayout.CENTER);
JButton printButton = new JButton("Print");
printButton.addActionListener.(
EventHandler.create(ActionListener.class, table, "print")
);
JPanel buttonPanel = new JPanel();
buttonPanel.add(printButton);
add(buttonPanel, BorderLayout.SOUTH);
pack();
};
};
How the table is displayed: