我设计了一个GUI,其中我使用了一个JTable,我必须使2个列不可见。我该怎么办?
从TableColumn
删除TableColumnModel
。
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn( tcm.getColumn(...) );
如果您需要访问数据,那么您使用table.getModel().getValueAt(...)
。
对于允许用户隐藏/显示列的更复杂的解决方案,请查看Table Column Manager。
首先从视图中删除列
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时,尝试使用Tab键,当你点击隐藏列时,焦点会消失,直到你再次选中。这会让用户感到困惑。”
我有同样的问题,因为我使用TableColumnModel
removColumn();
没有帮助我所以我用这个
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));
如果要访问已删除列的值,则必须通过getValueAt
的TableModel
函数访问它,而不是JTable
。但是你必须在模型中将rowIndex转换回rowIndex。
例如,如果要访问所选行的第一列:
int modelRow = table.convertRowIndexToModel(table.getSelectedRow());
int value = (Integer)table.getModel().getValueAt(modelRow,0);
将min,max和“normal”宽度设置为0:
jTable.getColumn("ABC").setMinWidth(0); // Must be set before maxWidth!!
jTable.getColumn("ABC").setMaxWidth(0);
jTable.getColumn("ABC").setWidth(0);
注意:由于你不能设置maxWidth
<minWidth
,你需要先改变minWidth
(javadoc)。 width
也是如此。
第二种方法是扩展TableColumnModel
并覆盖所有方法来创建幻觉(对于JTable),您的模型没有这两列。
因此,当您隐藏列时,必须在表格要求列数时少返回一列,并且当它要求列X时,您可能必须向列索引添加+1(取决于它是否在左侧)或隐藏列的权利)等
让您的新表模型将所有方法调用(带有更正的索引等)转发到实际的列模型,并在JTable中使用新的表模型。
您可以创建模型的子类,并按如下方式覆盖TableModel.getColumnCount
:
int getColumnCount() {
return super.getColumnCount()-2;
}
最后两列不会显示在JTable
中。
我已经尝试过所有这些:他们没有帮助。最好的方法是创建一个没有要删除的列的新表模型。这是你如何做到的:
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);