Remove jtable column
Hi all,
is there any way to remove a jtable's column passing as param the column name?
I'm using this right now:
table.getColumnModel().removeColumn(table.getColumnModel().getColumn(3));
but I want to make it in a generic way.
I have on this table, columns representing thename,description andtype of the object, and I want to remove the columns representing the type and the description, but I don't like the idea of passing anint to the method cuz if in the future I change the columns positions, this won't work anymore and I'll have to go back there and change the columns indexes by myself again...
Is there a way of removing a column passing the column name or something like that, to the method?
[782 byte] By [
Valerianoa] at [2007-11-27 11:25:20]

I'm not expert using Swing,
but I think you can iterate with the indexes until you find the name of the column you want.
Like (pseudo code)
for (Column col : table.getColumnModel.getColumns){
if (col.getTitle().equals("Type"))
table.getColumnModel.remove(col);
}
> Try something like this:
> > jTable.getColumnModel().removeColumn(jTable.getColumnE
> xt("Name"));
This method does not appear on sun's api.
> > Try something like this:
> > > >
> jTable.getColumnModel().removeColumn(jTable.getColumnE
>
> > xt("Name"));
>
> This method does not appear on sun's api.
Ahhh sorry. Meant to add that the above code is for a JXTable from the SwingX API and not the standard Java API. Many apologies
For a standard java api way of acheiving the same thing you could try the following:
while (orderTable.getColumnModel().getColumns().hasMoreElements()) {
TableColumn col = orderTable.getColumnModel().getColumns().nextElement();
if (col.getHeaderValue().equals("Name")) {
orderTable.getColumnModel().removeColumn(col);
break;
}
}
Bear in mind though, that if the column name you try to remove does not exist you will be stuck in an infinite loop
Message was edited by:
c0demonk3y
Correcting formatting