[英]How to make a columns in JTable Invisible for Swing Java
I have designed one GUI in which I have used one JTable from which I have to make 2 columns invisible . 我设计了一个GUI,其中我使用了一个JTable,我必须使2个列不可见。 How should I do that ?
我该怎么办?
Remove the TableColumn
from the TableColumnModel
. 从
TableColumn
删除TableColumnModel
。
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn( tcm.getColumn(...) );
If you need access to the data then you use table.getModel().getValueAt(...)
. 如果需要访问数据,则使用
table.getModel().getValueAt(...)
。
For a more complex solution that allows the user to hide/show columns as they wish check out the Table Column Manager . 对于允许用户根据需要隐藏/显示列的更复杂的解决方案,请查看表列管理器 。
First remove the column from the view 首先从视图中删除列
table.removeColumn(table.getColumnModel().getColumn(4));
Then retrieve the data from the model. 然后从模型中检索数据。
table.getModel().getValueAt(table.getSelectedRow(),4);
One thing to note is that when retrieving the data, it must be retrieve from model not from the table. 需要注意的一点是,在检索数据时,必须从模型中检索而不是从表中检索数据。
I tried 2 possible solutions that both work, but got some issue with the 1st solution. 我尝试了两种可行的解决方案,但都解决了第一种解决方案。
table.removeColumn(table.getColumnModel().getColumn(4));
or 要么
table.getColumnModel().getColumn(4).setMinWidth(0);
table.getColumnModel().getColumn(4).setMaxWidth(0);
table.getColumnModel().getColumn(4).setWidth(0);
In my recent case, I preferred the 2nd solution because I added a TableRowSorter . 在我最近的案例中,我更喜欢第二种解决方案,因为我添加了一个TableRowSorter 。
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
When using table.removeColumn(table.getColumnModel().getColumn(4))
, it will physically remove the column from the view/table so you cannot use table.getValueAt(row, 4)
- it returns ArrayIndexOutOfBounds
. 当使用
table.removeColumn(table.getColumnModel().getColumn(4))
,它将从视图/表中物理删除列,因此您不能使用table.getValueAt(row, 4)
- 它返回ArrayIndexOutOfBounds
。 The only way to get the value of the removed column is by calling table.getModel().getValueAt(table.getSelectedRow(),4)
. 获取已删除列的值的唯一方法是调用
table.getModel().getValueAt(table.getSelectedRow(),4)
。 Since TableRowSorter sorts only what's on the table but not the data in the DefaultTableModel object, the problem is when you get the value after sorting the records - it will retrieve the data from DefaultModelObject , which is not sorted. 由于TableRowSorter只对表中的内容进行排序,而不对DefaultTableModel对象中的数据进行排序,因此问题是在对记录进行排序后获取值时 - 它将从DefaultModelObject中检索未排序的数据。
So I used the 2nd solution then used table.getValueAt(table.getSelectedRow(),4);
所以我使用了第二个解决方案然后使用了
table.getValueAt(table.getSelectedRow(),4);
The only issue I see with the 2nd approach was mentioned by @camickr: "When you set the width to 0, try tabbing, when you hit the hidden column focus disappears until you tab again. This will confuse users." 我看到第二种方法的唯一问题是@camickr提到: “当你将宽度设置为0时,尝试使用Tab键,当你点击隐藏列时,焦点会消失,直到再次选中。这会让用户感到困惑。”
i had the same problem and because of i am using TableColumnModel
removColumn();
我有同样的问题,因为我正在使用
TableColumnModel
removColumn();
does'not help me so i used this 并没有帮助我所以我用这个
table.getColumnModel().getColumn(0).setWidth(0);
table.getColumnModel().getColumn(0).setMinWidth(0);
table.getColumnModel().getColumn(0).setMaxWidth(0);
and worked fine for me it hide a column 0 and i still able to get value from it 并且对我来说很好,它隐藏了第0列,我仍然可以从中获得价值
If you remove the column from the JTable
the column is still present in the TableModel
. 如果从
JTable
删除该列,则该列仍存在于TableModel
。
For example to remove the first ID column: 例如,要删除第一个ID列:
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn(tcm.getColumn(0));
If you want to access the value of the removed column, you have to access it through the getValueAt
function of the TableModel
, not the JTable
. 如果要访问已删除列的值,则必须通过
TableModel
的getValueAt
函数访问它,而不是JTable
。 But you have to convert to rowIndex back to rowIndex in the model. 但是你必须在模型中将rowIndex转换回rowIndex。
For example if you want to access the first column of the selected row: 例如,如果要访问所选行的第一列:
int modelRow = table.convertRowIndexToModel(table.getSelectedRow());
int value = (Integer)table.getModel().getValueAt(modelRow,0);
Set the min, max and "normal" width to 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);
Note: Since you can't set a maxWidth
< minWidth
, you need to change minWidth
, first ( javadoc ). 注意:由于无法设置
maxWidth
< minWidth
,因此需要先更改minWidth
( javadoc )。 Same is true for width
. width
也是如此。
The second approach is to extend TableColumnModel
and override all the methods to create the illusion (for the JTable) that your model doesn't have those two columns. 第二种方法是扩展
TableColumnModel
并覆盖所有方法,以创建模型没有这两列的错觉(对于JTable)。
So when you hide a column, you must return one less when the table asks for the number of columns and when it asks for the column X, you may have to add +1 to the column index (depending on whether it is to the left or right of the hidden column), etc. 因此,当您隐藏列时,必须在表格要求列数时少返回一列,并且当它要求列X时,您可能必须向列索引添加+1(取决于它是否在左侧)或隐藏列的权利)等
Let your new table model forward all method calls (with the corrected indexes, etc) to the actual column model and use the new table model in the JTable. 让您的新表模型将所有方法调用(带有更正的索引等)转发到实际的列模型,并在JTable中使用新的表模型。
I have tried them all: they don't help. 我已经尝试过所有这些:他们没有帮助。 The BEST way ever is to make a new table model without the column you want to delete.
最好的方法是创建一个没有要删除的列的新表模型。 This is how you do it:
这是你如何做到的:
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);
You could create a subclass of the model, and override TableModel.getColumnCount
as follows: 您可以创建模型的子类,并重写
TableModel.getColumnCount
,如下所示:
int getColumnCount() {
return super.getColumnCount()-2;
}
The last two columns would then not be displayed in the JTable
. 最后两列不会显示在
JTable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.