简体   繁体   中英

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

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.

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. As stated in the javadoc of that method

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.

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

You can hide it by setting its width to 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.

So for example the getColumnCount you give it as 3

and on getValueAt - to skip if the column index is above the skipped column

JTable will first call getColumnCount and getRowCount and fetch calling getValueAt for each of the cells.

NOTE: Ignore this and see link by trashgod below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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