我设计了一个GUI界面,其中使用了一个JTable表格,现在我需要将其中的2列设置为不可见。请问应该如何实现?
从 TableColumnModel
中移除 TableColumn
。
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn( tcm.getColumn(...) );
如果您需要访问数据,则使用table.getModel().getValueAt(...)
。首先从视图中移除该列
table.removeColumn(table.getColumnModel().getColumn(4));
然后从模型中检索数据。
table.getModel().getValueAt(table.getSelectedRow(),4);
需要注意的一点是,在检索数据时,必须从模型中检索而不是从表格中检索。
我尝试了两种可能的解决方案,它们都有效,但在第一个解决方案中遇到了一些问题。
table.removeColumn(table.getColumnModel().getColumn(4));
或者 table.getColumnModel().getColumn(4).setMinWidth(0);
table.getColumnModel().getColumn(4).setMaxWidth(0);
table.getColumnModel().getColumn(4).setWidth(0);
在我最近的情况下,我更喜欢第二种解决方案,因为我添加了一个TableRowSorter。
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
使用table.removeColumn(table.getColumnModel().getColumn(4))
会从视图/表中实际删除列,因此您不能使用table.getValueAt(row, 4)
- 它返回ArrayIndexOutOfBounds
。获取已删除列的值的唯一方法是调用table.getModel().getValueAt(table.getSelectedRow(),4)
。由于TableRowSorter仅对表上的内容进行排序,而不对DefaultTableModel对象中的数据进行排序,所以问题出现在在排序记录后获取该值时 - 它将从未排序的DefaultModelObject检索数据。
因此我使用了第二种解决方案,然后使用table.getValueAt(table.getSelectedRow(),4);
我看到第二种方法的唯一问题是由@camickr提到的:"当您将宽度设置为0时,请尝试制表符,在隐藏的列命中之前,焦点消失,直到再次制表符。这将使用户困惑。"
将最小、最大和“正常”宽度设置为0:
jTable.getColumn("ABC").setMinWidth(0); // Must be set before maxWidth!!
jTable.getColumn("ABC").setMaxWidth(0);
jTable.getColumn("ABC").setWidth(0);
maxWidth
小于minWidth
,因此您需要首先更改minWidth
。同样适用于width
。(参见javadoc)TableColumnModel
并覆盖所有方法,以创建假象(对于JTable)使您的模型没有这两列。我曾经遇到同样的问题,因为我使用了TableColumnModel
,所以removeColumn();
无法解决我的问题,所以我使用了以下方法:
table.getColumnModel().getColumn(0).setWidth(0);
table.getColumnModel().getColumn(0).setMinWidth(0);
table.getColumnModel().getColumn(0).setMaxWidth(0);
这对我来说很好用,它隐藏了第0列,但我仍然能够获取其中的值。
如果您从JTable
中删除列,则该列仍然存在于TableModel
中。
例如,要删除第一个ID列:
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn(tcm.getColumn(0));
如果您想访问已删除列的值,则必须通过TableModel
的getValueAt
函数访问它,而不是通过JTable
。但是,您必须将rowIndex转换回模型中的rowIndex。
例如,如果您想访问所选行的第一列:
int modelRow = table.convertRowIndexToModel(table.getSelectedRow());
int value = (Integer)table.getModel().getValueAt(modelRow,0);
我已经尝试了所有方法:它们都没有帮助。最好的方法是创建一个新的表模型,不包含您想要删除的列。以下是操作步骤:
table = (DefaultTableModel) <table name>.getModel();
DefaultTableModel table1 = new DefaultTableModel();
Vector v = table.getDataVector();
Vector v1 = newvector(v,<column index you want to delete>);
Vector newvector(Vector v,int j){
Vector v1= new Vector();
try{
Vector v2;
Object[] o = v.toArray();
int i =0;
while(i<o.length){
v2 = (Vector) o[i];
v2.remove(j);
v1.add(v2);
i++;
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error in newvector \n"+e);
}
return v1;
}
Vector getColumnIdentifiers(int i) {
Vector columnIdentifiers = new Vector();
int j=0;
while(j<i){
columnIdentifiers.add(("c"+(j+1)));
j++;
}
return columnIdentifiers;
}
table1.setDataVector(v1,getColumnIdentifiers((<column count>-1)));
<table name>.setModel(table1);
TableModel.getColumnCount
方法,如下所示:int getColumnCount() {
return super.getColumnCount()-2;
}
然后,最后两列将不会在JTable
中显示。
convertXXX
方法可供使用。因此,你可以使用convertColumnIndexToModel(...)
方法。 - camickr