java 如何隐藏 DefaultTableModel 中的特定列而不显示在表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12195973/
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 hide a particlar column in DefaultTableModel from displaying it in table?
提问by brainless
I am using Java Swingx framework. I have 4 columns in my DefaultTableModel
object. I wish to display only 3 of the columns. But, I need all four for computing.
我正在使用 Java Swingx 框架。我的DefaultTableModel
对象中有 4 列。我希望只显示 3 列。但是,我需要所有四个来计算。
Actual data model
实际数据模型
S.No. | ID | GDC ID | Decsription
S.No. | ID | GDC ID | Decsription
What I want to display in table
我想在表格中显示什么
S.No.| GDC ID | Decsription
S.No.| GDC ID | Decsription
Is it possible to hide or omit only one column from rendering? Please guide me.
是否可以在渲染中仅隐藏或省略一列?请指导我。
回答by mKorbel
by default minimum size is
10 pixels
widht,you can to remove / add column from JTable view, column presents in the XxxTableModel, you can to hide and show any of column(s)
默认最小尺寸为
10 pixels
宽,您可以从 JTable 视图中删除/添加列,列出现在 XxxTableModel 中,您可以隐藏和显示任何列
回答by Robin
No need to adjust your model, or to try to make that column very small. JTable
has built-in functionality for this: the removeColumn
method. As stated in the javadoc of that method
无需调整您的模型,也无需尝试将该列变得非常小。JTable
为此具有内置功能:removeColumn
方法。如该方法的javadoc所述
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.
从此 JTable 的列数组中删除 aColumn。注意:此方法不会从模型中删除数据列;它只是删除了负责显示它的 TableColumn。
Also note the existence of the following methods:
还要注意以下方法的存在:
Since the column order and column count in the view (the JTable
) might be different from the one in the model you need those methods to switch between view and model
由于视图 (the JTable
) 中的列顺序和列数可能与模型中的不同,因此您需要这些方法在视图和模型之间切换
回答by Dan D.
You can hide it by setting its width to 0.
您可以通过将其宽度设置为 0 来隐藏它。
_table.getColumn("ID").setPreferredWidth(0);
_table.getColumn("ID").setMinWidth(0);
_table.getColumn("ID").setWidth(0);
_table.getColumn("ID").setMaxWidth(0);
回答by august0490
try this to remove a single column:
试试这个来删除单列:
myTableModel = new DefaultTableModel();
myTableModel.setColumnIdentifiers(new Object[]{"S.No.", "ID", "GDC ID", "Decsription"});
JTable myTable = new JTable(myTableModel);
// remember to save the references
TableColumn myTableColumn0 = guiLoteryNumbersTable.getColumnModel().getColumn(0);
TableColumn myTableColumn1 = guiLoteryNumbersTable.getColumnModel().getColumn(1);
TableColumn myTableColumn2 = guiLoteryNumbersTable.getColumnModel().getColumn(2);
TableColumn myTableColumn3 = guiLoteryNumbersTable.getColumnModel().getColumn(3);
myTable.getColumnModel().removeColumn(myTableColumn1);
Then to show the column again keeping the order:
然后再次显示该列,保持顺序:
// 1- remove all the existent columns
myTable.getColumnModel().removeColumn(myTableColumn0);
myTable.getColumnModel().removeColumn(myTableColumn2);
myTable.getColumnModel().removeColumn(myTableColumn3);
// 2- add all the columns again in the right order
myTable.getColumnModel().addColumn(myTableColumn0);
myTable.getColumnModel().addColumn(myTableColumn1);
myTable.getColumnModel().addColumn(myTableColumn2);
myTable.getColumnModel().addColumn(myTableColumn3);
Sorry, but that's the best way I know.
对不起,但这是我所知道的最好的方式。
回答by Sid Malani
You manipulate the getValueAt , getColumnCount to achieve this.
您可以操作 getValueAt 、 getColumnCount 来实现这一点。
So for example the getColumnCount you give it as 3
因此,例如您给它的 getColumnCount 为 3
and on getValueAt - to skip if the column index is above the skipped column
并在 getValueAt - 如果列索引高于跳过的列,则跳过
JTable will first call getColumnCount and getRowCount and fetch calling getValueAt for each of the cells.
JTable 将首先调用 getColumnCount 和 getRowCount 并为每个单元格调用 getValueAt 获取。
NOTE: Ignore this and see link by trashgod below.
注意:忽略这一点并查看下面垃圾神的链接。