Possible Duplicate:
How to make a columns in JTable Invisible for Swing Java
How to hide a particlar column in DefaultTableModel from displaying it in table?
我想在 JTable 的三列中输入数据,但我只想显示两列。实际上,我想隐藏第三列,不是通过将宽度设置为 0,而是通过任何其他方法,我可以通过单击事件从隐藏列中获取数据。
如何以这种方式隐藏列?
我正在使用以下代码:
try {
String Title[]= new String{"a","b","c"};
Object obj= new Object[50][3];
JTable table= new JTable(obj,title);
JScrollPane jsp= new JScrollPane(table);
add(jsp);
} catch(Exception ex) {
ex.printStackTrace();
}
将列的最小和最大宽度设置为零。
table.getColumnModel().getColumn(columnIndex).setMinWidth(0);
table.getColumnModel().getColumn(columnIndex).setMaxWidth(0);
正如 Andrew Thomson 在评论部分所建议的,您也可以使用 removeColumn .
来自javaDoc;
removeColumn
public void removeColumn(TableColumn aColumn)
Removes aColumn from this JTable's array of columns. Note: this method does
not remove the column of data from the model; it just removes the
TableColumn that was responsible for displaying it. Parameters:
aColumn - the TableColumn to be removed
P.S: 但我个人使用第一种方法来隐藏 JTable
中的列。感谢 removeColumn
方法,我将从现在开始尝试使用它。