简体   繁体   English

如何隐藏DefaultTableModel中的一个特殊列,使其不在表中显示?

[英]How to hide a particlar column in DefaultTableModel from displaying it in table?

I am using Java Swingx framework. 我正在使用Java Swingx框架。 I have 4 columns in my DefaultTableModel object. 我的DefaultTableModel对象中有4列。 I wish to display only 3 of the columns. 我希望只显示3列。 But, I need all four for computing. 但是,我需要所有四个用于计算。

Actual data model 实际数据模型

S.No. | ID | GDC ID | Decsription

What I want to display in table 我想在表格中显示什么

S.No.| GDC ID | Decsription

Is it possible to hide or omit only one column from rendering? 是否可以隐藏或省略渲染中的一列? Please guide me. 请指导我。

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. JTable具有内置功能: removeColumn方法。 As stated in the javadoc of that method 正如该方法的javadoc中所述

Removes aColumn from this JTable's array of columns. 从此JTable的列数组中删除aColumn。 Note: this method does not remove the column of data from the model; 注意:此方法不会从模型中删除数据列; it just removes the TableColumn that was responsible for displaying it. 它只是删除了负责显示它的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 由于视图中的列顺序和列数( JTable )可能与模型中的列顺序和列数不同,因此您需要这些方法在视图和模型之间切换

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);

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. 对不起,但这是我所知道的最好的方式。

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. 注意:忽略此信息并通过下面的trashgod查看链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM