Hiding columns in a JTable

I have a hard time hiding columns in a JTable. Every time I try to hide the columns I wanted, just the columns headers gets hidden but the actual column associted with those headers remain visible. However, the columns towards the end disappers yielding the right column count. For example if I want to hide col3, and col 4 of a table consisting of 7 columns, the column headers of col3 and col4 disappears but not the actual column(data) itself. To my great surprise, the data for last two columns disappears( not the column headers). I use my own DefaultTableCellRenderer (MyPopulateTableCellRenderer class)to populate my table.

When I remove columns I do this:

myJTable.setDefaultRenderer(MyRecordInfo.class, new MyPopulateTableCellRenderer() );

myJTable.removeColumn(myJTable.getColumn("Col 3"));

myJTable.removeColumn(myJTable.getColumn("Col 4"));

Can anyone please tell me what am I doing wrong?

[940 byte] By [ap68847] at [2007-9-27 19:06:10]
# 1
can you post your complete code? I done the similar thing, but I was using CustomTableModel extending AbstractTableModel. It worked fine for meCheers,Sekar
rajaboobalan at 2007-7-6 21:21:12 > top of Java-index,Archived Forums,Swing...
# 2
If you really want to hide a column, not remove it completely, what you should be doing is to set the column width to zero.;o)V.V.
viravan at 2007-7-6 21:21:12 > top of Java-index,Archived Forums,Swing...
# 3

If you have an own TableModel, the easiest way is just to add

a scope of hiding the columns you need.

Ie: Methods like getColumnCount(), getValueAt(...) ensure that

they don't count the hidden columns.

Makes it very easy to hide and extend later on.

A better solution is even to make an HiddenTableModel that you push an normal TableModel in, and it will take care of the hiding the columns, much like the decorator pattern. The only problem is that you will have to mask the event-handler making sure that some events are updated correctly.

Also with this type of function it will easily hide rows as well as columns.

I've done both solutions, the later one is much nicer, but I am afraid I have not the code here at the moment.

bithir at 2007-7-6 21:21:12 > top of Java-index,Archived Forums,Swing...
# 4
try this:myJTable.removeColumn(myJTable.getColumn("Col 3").getModelIndex());myJTable.removeColumn(myJTable.getColumn("Col 4").getModelIndex()); There are two seperate indices... one is the display index the other is the model index.Hope this helps.Paul
pmaurer at 2007-7-6 21:21:12 > top of Java-index,Archived Forums,Swing...
# 5
Thank you all. I implemented pmaurer's solution. It worked for me.
ap68847 at 2007-7-6 21:21:12 > top of Java-index,Archived Forums,Swing...