Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding a column to a JTable

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that extends DefaultTablecolumnModel and one that extends AbstractTableModel. I create an instance of my class that extends JTable passing these in as column model and table model.

My table model has a list of 6 possible columns; 4 of those are displayed correctly on startup. I have a UI that allows a user to choose to display another column; when it is invoked, my code goes through the list of columns that are supposed to be displayed with the possible columns, adding any not there and removing any that are there but not selected. My last column is to remain the last column, so any added column is moved to last-1 by this code.

It's all working, except that my added column is not getting its correct class returned, and therefore is not formatted correctly (It is an Integer column, but doesn't get its right-justification). I put a trace statement in to echo the column number class being returned from getColumnClass, and it appears that, after adding a column in, the table model still thinks that column is the last one.

If the user resizes the column, THEN it gets formatted correctly.

I don't know whether I'm failing to update something (I thought addColumn in the ColumnModel was taking care of that) or whether I'm supposed to translate the view index to the model index, or what.

One other oddity -- if I don't move the newly added column (with JTable.moveColumn()), it gets added correctly to the right hand side of the table, but I can't see it until I resize the window.

So I'm looking for a better conceptual model of what's supposed to be happening here. It's hard to believe I'm supposed to use a method from JTable within the TableModel's getColumnClass() - I don't have a lot of reason for a reference to the table in that object otherwise. What am I missing here?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic