JTable隐藏和显示列
问题描述:
我想在表中添加一些列(Swing JTable)。其中一些将具有默认大小(例如250),其他将被隐藏(因此它们的大小将为0)。我使用这段代码:
I want to add some columns to a table (Swing JTable). Some of them will have a default size (e.g. 250), others will be hidden (so their size will be 0). I use this code:
model = new DefaultTableModel();
table = new JTable(model);
setAutoResizeMode(AUTO_RESIZE_OFF);
for (int i = 1; i < COLUMN_NAMES.length; i++) {
model.addColumn(COLUMN_NAMES[i]);
if (show[i]) show(index);
else hide(index);
}
........
private void hide(int index) {
TableColumn column = getColumnModel().getColumn(index);
column.setMinWidth(0);
column.setMaxWidth(0);
column.setWidth(0);
column.setPreferredWidth(0);
doLayout();
}
private void show(int index) {
final int width = 250;
column.setMinWidth(15);
column.setMaxWidth(width);
column.setWidth(width);
column.setPreferredWidth(width);
doLayout();
}
问题是当显示表格时,显示所有列(无是隐藏的),它们的大小不是250,但它们的大小都相同。
the problem is when the table is displayed, all the columns are showed (none is hidden) and their size is not 250 but they have all the same size.
我怎样才能得到想要的效果?
How can I get the wanted effect?
答
JTable #removeColumn 仅从JTable视图中删除列,更多内容在此示例
JTable#removeColumn remove Column only from JTable view, more in this example