Removing data from JTables.
Hi,
I'm having trouble removing data from a DefaultTableModel.
I'm trying to remove all columns on a JTable.
My current logic is...
for (int i = 1; i < jTable.getColumnCount() + 1; i++ )
{
TableColumnModel colModel = jTable.getColumnModel();
TableColumn col = colModel.getColumn(i);
colModel.removeColumn(col);
}
Which ofcourse is wrong. Any help would be appreciated.
It may be that you need to manipulate the table model. I have just had a look at the javadocs for the setColumCount() method of the DefaultTableModel class and it says this;
setColumnCount
public void setColumnCount(int columnCount)
Sets the number of columns in the model. If the new size is greater than the current size, new columns are added to the end of the model with null cell values. If the new size is less than the current size, all columns at index columnCount and greater are discarded.
That does not seem to allow you to remove columns from the middle of the table however. Having said that, I am still going to guess that if you wish to do so, you will have to manipultae the table model; for eaxmple, if you have a model that originally contained five columns of data and you wised to remove the second column, then I am going to guess that you would have to create a new Vector - or other collection - to hold the models data and just copy over the contents of the columns you wished to keep, reset the size of the table model - i.e. the column count - and then notify the table that the models data had changed.
"new" the table model or set the data to an empty vector or empty array.step back for a moment and ask yourself, "is there an easier way to accomplish what I want?"
Nope, If I attempt to do this...
if (jTable.getColumnCount() > 0 )
{
model = new DefaultTableModel();
model.fireTableStructureChanged();
}
I get a blank dialog message popup.
I feel that doing this, may be cheating anyway.
I just don't see why it should be so difficult.
> Nope, If I attempt to do this...
>
> > if (jTable.getColumnCount() > 0 )
> {
> model = new DefaultTableModel();
> model.fireTableStructureChanged();
> }
>
>
> I get a blank dialog message popup.
where in your original message does it say that you are using a dialog?
> I feel that doing this, may be cheating anyway.
>
> I just don't see why it should be so difficult.
It isn't that difficult.
By the way, 1982 was a good year:
Toni Basil - Mickey
Asia - Heat Of The Moment
John Cougar - Jack & Diane
Flock Of Seagulls - I Ran (So Far Away)
The Go-Go's - We Got The Beat
The Human League - Don't You Want Me
Joan Jett & The Blackhearts - I Love Rock 'n' Roll
Huey Lewis & The News - Do You Believe In Love
Stevie Nicks - Edge Of Seventeen
Survivor - Eye Of The Tiger
and yawmark's favorite...
Tommy Tutone - 867-5309/Jenny
I'm not explicity using a dialog, it just pops up with a blank message.
It's not difficult?Can you show me an example?
> Which of course is wrong. Any help would be appreciated.
Nobody has yet got around to explaining why it's wrong. Here's why:
Suppose you start out with six columns: A, B, C, D, E, F,
When you delete column 0, you now have five columns: B, C, D, E, F.
When you delete column 1, you delete the second of those five: B, D, E, F.
When you delete column 2, you delete the third of those four: B, D, F.
When you delete column 3, you would delete the fourth column, except that it doesn't exist.
I'm still not getting anywhere with this
DrClap:My understanding of the problem was different; the OP simply wanted to remove the columns such that the table was now an "empty" table. Not the selective removal of columns. Hence my suggestion to just have an empty data.OP: Could you clarify what you want to
> Read the OP.
It says:I'm having trouble removing data from a DefaultTableModel.
I'm trying to remove all columns on a JTable.
If you are removing all columns, then the table is a "nothing" table- for lack of a better word.
Again, what is it you are trying accomplish? Be explicit, please.
I think reply 7 was correct. Shouldn't it be something like this?
while(jTable.getColumnCount() > 0) {
TableColumnModel colModel = jTable.getColumnModel();
TableColumn col = colModel.getColumn(0);
colModel.removeColumn(col);
}
While there are still columns left, remove the first column.
If the intent is to remove columns sequentially, then I agree with DrClap and hunter. I still think the original query is unclear.
> DrClap:
>
> My understanding of the problem was different; the OP
> simply wanted to remove the columns such that the
> table was now an "empty" table. Not the selective
> removal of columns. Hence my suggestion to just have
> an empty data.
Well, indeed. There are all kinds of ways to interpret the post. I couldn't understand why you would want a table with zero columns, so I just tried to address the comprehensible part of the question.
> OP: Could you clarify what you want to accomplish?
Seconded. This smells like the second question to me, we haven't heard the first question which deleting all the columns was the answer to.
Thanks, that helped me lots.