我有一個JFrame,其中有多個類似寬度的JPanel,它們相互對齊。我使用其中一個JPanel來顯示一個JTable,它是該批次的最後一個JPanel。此JPanel將JScrollpane作爲子組件。這是我嘗試動態添加我的表格的地方。此JScrollPane的初始高度設置爲40如何在運行時更改jtable高度
我上面設計模板使用NetBeans 6.8
現在我試圖將表添加到JPanel中。當在代碼片段下面按下按鈕時被調用。包含此代碼的類擴展了javax.swing.JFrame類。
我期待下面的代碼將根據行數調整表高度並顯示錶。
SearchTable = new JTable(RowData, DisplayNames) {
@Override
public boolean isCellEditable(int rowIndex, int vColIndex) {
return false;
}
};
// if row count is less than 10 then display all the rows without a scroll bar
if (SearchTable.getRowCount() < 10) {
pnl_tblpanel.setPreferredSize(new Dimension(625, SearchTable.getRowHeight() * (SearchTable.getRowCount() + 4)));
scr_tblholder.setPreferredSize(new Dimension(625, SearchTable.getRowHeight() * (SearchTable.getRowCount() + 4)));
} else {// if row count is more than 10 display first 10 rows and add a scroll bar
pnl_tblpanel.setPreferredSize(new Dimension(625, SearchTable.getRowHeight() * (10 + 2)));
scr_tblholder.setAutoscrolls(true);
}
//pnl_tblpanel.add(scr_tblholder);
scr_tblholder.setViewportView(SearchTable);
//pnl_tblpanel.repaint();
pnl_tblpanel.validate();
this.validate();
//this.repaint();
pnl_tblpanel.setVisible(true);
this.pack();
該表顯示,但根據行計數表高度不發生變化。它保持其默認值。我一直在嘗試驗證和重繪的許多組合,但沒有任何工作。 (更多在絕望中)
任何人都可以闡明這 一些輕謝謝
什麼樣的佈局管理器是你的組件?僅僅因爲你設置了首選大小並不意味着佈局經理需要遵守它。 – 2010-05-14 14:13:49