I am filling a JTable with data from a database. I am have subclassed AbstractTableModel and passed the reference to the JTable. However, how do I manage to extract the column names in the database and set them as "headers" in the JTable?
Thanks in advance.
I already have this, in the console the header names display but I do not have a column in the GUI. I have attached the JTable to a tab in a tabbed pane.
@Override
public String getColumnName(int column) {
try {
System.out.println(dbhandler.getMetaData().getColumnName(column + 1));
return dbhandler.getMetaData().getColumnName(column + 1);
} catch (SQLException e) {
e.printStackTrace();
return "";
}
}
However, how do I manage to extract the column names in the database and set them as "headers" in the JTable?
Your table model needs to implement the getColumnName() method. Then you need to create your table model with data first. Then you create your JTable using the table model and the table will build the columns for you.
See Table from Database for code that provides a reusable table model as well as a method to populate the table for you automatically.
I do not have any columns at all, it is all rows...
You need to add the table to a JScrollPane (not a JPanel) and the headers will appear as the column header view of the scroll pane:
JScrollPane scrollPane = new JScrollPane( table );