• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable - Table Model vs Display View Columns

 
Roy Anderson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roy Anderson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic