jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes JTable - Table Model vs Display View Columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - Table Model vs Display View Columns" Watch "JTable - Table Model vs Display View Columns" New topic
Author

JTable - Table Model vs Display View Columns

Roy Anderson
Greenhorn

Joined: Oct 22, 2003
Posts: 4
When displaying a JTable, the user is able to rearrange the order of the columns. The isCellEditable method uses the display view column number instead of the table model column value. How do I access the cross reference between the display and table model columns? For example when the JTable is first displayed with 4 columns, Columns 1,2,3 are editable and column 0 is not editable.
The user then rearranges the columns so that columns 0 and 1 are switched. However the new column 0 is still not editable, because that is the column number delivered to isCellEditable method.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

At WorkZonie,
Welcome to Javaranch!
As you know this is a great community in which to learn Java and we don't have too many rules around here, but we do have a Naming Policy. Please read this policy and adjust your display name accordingly.
You can change your display name here.
Thanks.


GenRocket - A Test Data Generation Platform
Roy Anderson
Greenhorn

Joined: Oct 22, 2003
Posts: 4
Gregg:
Sorry about that, I was using an old screen name from when I signed up long ago. Did not know about the new rule. I have changed the display name to my real name. Thank you for the gentle reminder.
Roy.
Robert Tiffer
Greenhorn

Joined: Apr 21, 2004
Posts: 2
I have a table much like you've described, and it works as desired. The JTable javadoc says that column reorderings are tracked by the JTable and it uses the correct column number when querying the TableModel. Does your code do anything fancy that might interfere?
Roy Anderson
Greenhorn

Joined: Oct 22, 2003
Posts: 4
The table does not do anything special otherthan two of the columns have JComboBoxes. I think I have found the answer....
A column in a JTable component has two types of indices, a visible index and a model index. The visible index of a column is its visible location on the screen. The model index of a column is its permanent position in a TableModel, which contains the actual data. This example demonstrates how to convert a column index between the two forms.
// Converts a visible column index to a column index in the model.
// Returns -1 if the index does not exist.
public int toModel(JTable table, int vColIndex) {
if (vColIndex >= table.getColumnCount()) {
return -1;
}
return table.getColumnModel().getColumn(vColIndex).getModelIndex();
}

// Converts a column index in the model to a visible column index.
// Returns -1 if the index does not exist.
public int toView(JTable table, int mColIndex) {
for (int c=0; c<table.getColumnCount(); c++) {
TableColumn col = table.getColumnModel().getColumn(c);
if (col.getModelIndex() == mColIndex) {
return c;
}
}
return -1;
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JTable - Table Model vs Display View Columns
 
Similar Threads
JTable cell uneditable
jtable
JTable
JTable help
JTable