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]
# 1

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);

}

pbulgarellia at 2007-7-29 16:03:50 > top of Java-index,Java Essentials,New To Java...
# 2

Try something like this:

jTable.getColumnModel().removeColumn(jTable.getColumnExt("Name"));

c0demonk3ya at 2007-7-29 16:03:50 > top of Java-index,Java Essentials,New To Java...
# 3

> Try something like this:

> > jTable.getColumnModel().removeColumn(jTable.getColumnE

> xt("Name"));

This method does not appear on sun's api.

Valerianoa at 2007-7-29 16:03:50 > top of Java-index,Java Essentials,New To Java...
# 4

> > 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

c0demonk3ya at 2007-7-29 16:03:50 > top of Java-index,Java Essentials,New To Java...
# 5

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

c0demonk3ya at 2007-7-29 16:03:50 > top of Java-index,Java Essentials,New To Java...
# 6

>if the column name you try to remove does not exist you will be stuck in an infinite loop

why?

hasMoreElements() will return "false" when no more columns found.

and will exit while

Am I wrong?

pbulgarellia at 2007-7-29 16:03:50 > top of Java-index,Java Essentials,New To Java...