I have created an app just for exercise purpose with a JTable in it to show the data coming from my database.
My problem is that the table shows up in half the panel on my screen. I’ve tried everything to expand the columns without success. Can someone tell me how to modify the table?
Here is the code that creates the table:
public void createTable() {
JTable table = new JTable();
DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{},new String[]{"To do","Date added"});
table.setSize(450, 600);
table.setModel(tableModel);
for(int i = 0; i < model.getId().size(); i++) {
tableModel.addRow(new Object[]{model.getItem().get(i), model.getDate().get(i)});
}
add(table.getTableHeader());
add(table);
}
Here is how it looks right now:

Also I want to ask about table.getTableHeader() because if I don’t add that it doesn’t show up since it is a part of the table.. is that normal?
JPanelhasFlowLayoutimplemented in API, then yourJPanellayedJComponentscorrectly, with yours logics (addJTableHeader, addJTable)change that to the
BorderLayoutand to putJTableHeaderto theNORTH/SOUTHandJTableto theCENTERuse
JScrollPaneforJTables view, everything else is bothering withLayoutManagerand calculating forPreferredSizevery ugly way,