我有一個BoxLayout(在一個BorderLayout的面板中),我在其中放置了一個JTable和一個按鈕。我希望桌子有一個固定的寬度,並且高度應該調整自己以填充面板。目前,我對顯示的寬度沒有問題,但沒有填滿整個面板的高度。例如,如果表中有零個數據,我只想顯示列名。我嘗試過像setViewportView()和setPreferredSize()這樣的東西,但不能真正使它工作。有什麼建議麼?它在佈局還是滾動窗格中?Jtable in BoxLayout
String[] columnNames = { "Date", "Type"
};
Object[][] data = {
};
JTable myTable = new JTable(data, columnNames);
JButton okButton = new JButton("OK");
JPanel panWest = new JPanel();
panWest.setLayout(new BoxLayout(panWest, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(myTable);
panWest.add(scrollPane);
panWest.add(okButton);
編輯 這是結束了工作:
Dimension dimension = new Dimension();
dimension.height = (myTable.getRowCount()*myTable.getRowHeight())+
myTable.getTableHeader().getPreferredSize().height;
dimension.width = myTable.getColumnModel().getTotalColumnWidth();
scrollPane.setMaximumSize(dimension);
對於b etter幫助更快,發佈[SSCCE](http://sscce.org/)。 –