I want to make the table width at least fitting the enclosing scroll pane's width, and if it's getting larger, the horizontal scroll bar is used.
None of JTable
's auto resize modes suits my need: AUTO_RESIZE_OFF
does allow the table to be resized larger than the scroll pane's width, but it can't limit the table's minimal width; the other modes don't allow the table width to be larger than the scroll pane's width at all.
I have tried to set the minimal size as below but it didn't work:
JTable table = new JTable(myModel);
JScrollPane scrollPane = new JScrollPane(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// After the table is displayed.
table.setMinimalSize(scrollPane.getSize());
Any idea to achieve that?