我设计了一个GUI,其中我使用了一个JTable,我必须使2个列不可见.我该怎么办?
我想在表中添加一些列(Swing JTable).其中一些将具有默认大小(例如250),其他将被隐藏(因此它们的大小将为0).我用这个代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
问题是当显示表格时,显示所有列(没有隐藏),它们的大小不是250,但它们具有相同的大小.
我怎样才能得到想要的效果?
我试图创建一个列JTable,通过将宽度设置为零而不可见,但它不会发生,并且它仍然可见宽度= 15.这是代码 -
public void restoreColumnWithWidth(int column, int width) {
try {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
table.getTableHeader().setResizingColumn(tableColumn);
tableColumn.setWidth(width);
tableColumn.setMaxWidth(width);
tableColumn.setMinWidth(width);
tableColumn.setPreferredWidth(width);
} catch (Exception ex) {
}
}
Run Code Online (Sandbox Code Playgroud)
代码有什么问题?