如何使 JTable 中的列对 Swing Java 不可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1492217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a columns in JTable Invisible for Swing Java
提问by om.
I have designed one GUI in which I have used one JTable from which I have to make 2 columns invisible . How should I do that ?
我设计了一个 GUI,其中我使用了一个 JTable,我必须从中隐藏 2 列。我该怎么做?
回答by Aaron Digulla
Set the min, max and "normal" width to 0:
将最小、最大和“正常”宽度设置为 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). Same is true for width
.
注意:由于您不能设置maxWidth
< minWidth
,您需要先更改minWidth
,(javadoc)。也是如此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 中使用新表模型。
回答by Maurice Perry
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
.
回答by camickr
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.
对于允许用户根据需要隐藏/显示列的更复杂的解决方案,请查看表列管理器。
回答by Mr. Xymon
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
. The only way to get the value of the removed column is by calling table.getModel().getValueAt(table.getSelectedRow(),4)
. Since TableRowSortersorts only what's on the table but not the data in the DefaultTableModelobject, the problem is when you get the value aftersorting the records - it will retrieve the data from DefaultModelObject, which is not sorted.
使用时table.removeColumn(table.getColumnModel().getColumn(4))
,它将从视图/表中物理删除该列,因此您无法使用table.getValueAt(row, 4)
- 它返回ArrayIndexOutOfBounds
. 获取已删除列的值的唯一方法是调用table.getModel().getValueAt(table.getSelectedRow(),4)
. 由于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 键,当您点击隐藏列时,焦点会消失,直到您再次使用 Tab 键为止。这会让用户感到困惑。”
回答by Rijul Gupta
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);
回答by zawhtut
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.
需要注意的一件事是,在检索数据时,必须从模型而不是从表中检索。
回答by Anti Atlas Dev
i had the same problem and because of i am using TableColumnModel
removColumn();
does'not help me so i used this
我有同样的问题,因为我正在使用对我没有TableColumnModel
removColumn();
帮助所以我使用了这个
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 列,我仍然能够从中获得价值
回答by Foltin
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
. But you have to convert to rowIndex back to rowIndex in the model.
如果要访问已删除列的值,则必须通过 的getValueAt
函数访问它TableModel
,而不是JTable
。但是您必须在模型中将 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);