File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes adding a column to a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding a column to a JTable" Watch "adding a column to a JTable" New topic
Author

adding a column to a JTable

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding a column to a JTable
 
Similar Threads
jtable
unable to insert JRadio in JTable.
Wanted: conceptual tutorial on JTable
JTable and Custom Columns
JTable