Hank
Hank

Reputation: 3497

JTable will not auto resize last column

I have a JTable that is placed inside a JPanel which is then placed in a JFrame that has another JPanel that has a ScrollPane. The general idea can be seen below if the explanation is confusing. I have set my JTable to auto resize the last column, but it never auto sizes. What is the problem?

JFrame -> JPanel -> JTable
-> JPanel -> Scroll Pane

My code:
this refers to my class which extends JFrame

this.setLayout(new BorderLayout());

JTextArea log = new JTextArea(20, 50);
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);

String[] configRow = {"Config File", "Not Loaded"};
String[] logRow = {"Log File" , "Not Loaded"};
DefaultTableModel dtm = new DefaultTableModel();
dtm.addColumn("");
dtm.addColumn("");
dtm.addRow(configRow);
dtm.addRow(logRow);

JTable status = new JTable(dtm);
status.setTableHeader(null);
status.setShowGrid(false);
status.setEnabled(false);
status.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BorderLayout());
tablePanel.add(status, BorderLayout.LINE_START);

JPanel logPanel = new JPanel();
logPanel.add(logScrollPane);

this.add(tablePanel, BorderLayout.NORTH);
this.add(logPanel, BorderLayout.SOUTH);

Upvotes: 0

Views: 3011

Answers (1)

wolfcastle
wolfcastle

Reputation: 5930

Your JTable must be placed inside a JScrollPane in order for the resizing to work properly. You are placing the JTable directly on a JPanel.

tablePanel.add(new JScrollPane(status), BorderLayout.LINE_START):

Upvotes: 2

Related Questions