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

5

2

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.

Roband answered 30/8, 2012 at 11:34 Comment(1)
possible duplicate of How to make a columns in JTable Invisible for Swing JavaChime
B
4
Bacciform answered 30/8, 2012 at 11:41 Comment(0)
K
4

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

Krohn answered 30/8, 2012 at 12:7 Comment(0)
V
3

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);
Vmail answered 30/8, 2012 at 11:37 Comment(1)
There is built-in functionality to 'hide' a column from the view, which is better then meddling with the sizeKrohn
M
1

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.

Murrelet answered 27/1, 2015 at 12:49 Comment(0)
D
0

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.

Digitalize answered 30/8, 2012 at 11:38 Comment(4)
Right, the JTable view displays only what the TableModel provides; see also this alternative.Silsby
You shouldn't touch/alter the TableModel for this as the JTable has this functionality built-in. See my answer or @Silsby link. If you go for the model-modification, I suggest to use a decoratorKrohn
@Krohn - agree - i think thats a better solution.Digitalize
@Robin: Yes, I overlooked the use of DefaultTableModel. Your and mKorbel's answer will likely prove easier in that context. I commonly use Sid's approach in a subclass of AbstractTableModel. Depending on usage, brainless may yet want to consider this option.Silsby

© 2022 - 2024 — McMap. All rights reserved.