我有一个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