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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
brainless
  • 5,698
  • 16
  • 59
  • 82
  • possible duplicate of [How to make a columns in JTable Invisible for Swing Java](http://stackoverflow.com/questions/1492217/how-to-make-a-columns-in-jtable-invisible-for-swing-java) – fglez Apr 03 '13 at 15:09

5 Answers5

4
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
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

Robin
  • 36,233
  • 5
  • 47
  • 99
2

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);
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • 2
    There is built-in functionality to 'hide' a column from the view, which is better then meddling with the size – Robin Aug 30 '12 at 12:10
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.

august0490
  • 777
  • 1
  • 9
  • 15
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.

Sid Malani
  • 2,078
  • 1
  • 13
  • 13
  • Right, the `JTable` view displays only what the `TableModel` provides; see also this [alternative](http://stackoverflow.com/a/10089138/230513). – trashgod Aug 30 '12 at 11:46
  • 1
    You shouldn't touch/alter the `TableModel` for this as the `JTable` has this functionality built-in. See my answer or @trashgod link. If you go for the model-modification, I suggest to use a decorator – Robin Aug 30 '12 at 12:08
  • @Robin - agree - i think thats a better solution. – Sid Malani Aug 30 '12 at 12:19
  • @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. – trashgod Aug 30 '12 at 13:09