4

Say I have a JTable with some data in it. A call to table.getValueForCell(row,col) returns the contents of the cell. This is as expected.

Now say I want to sort my table. I click the table header (column), and it sorts the table. If I make a call to table.getValueForCell(row,col) with the same values for row and col. There is now a different row here because the table was sorted. However, the table.getValueForCell(row,col) returns the old data. It seems as if the underlying data structures that hold the table data are not updated when sorting.

Any idea on how to fix this, or what I might be doing wrong?

2 Answers 2

5

The data is always stored in its original order in the TableModel. You can access the original order using:

table.getModel().getValueAt(...);

Whenever the table is sorted, only the view changes. When you get data from the table you just use:

table.getValueAt(...);

If you some reason you need to convert back and forth between the two you can use either of the approriate table methods:

convertRowIndexToModel(...); 
convertRowIndexToView(...); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this was informative. Let me see if I understand correctly. If you want the data as it was originally put into the table, use table.getMode().getValueAt(). If you want the data as it currently appears in the table, use table.getValueAt()?
you need convertRowIndexToModel(...) when obtaining row numbers from table.getSelectedRows() (which are in 'view space') and want to use them with the model (whose indices are in 'model space'), see also the TableRowSorter apidoc
2

From the documentation for JTable.

The JTable uses integers exclusively to refer to both the rows and the columns of the model that it displays. The JTable simply takes a tabular range of cells and uses getValueAt(int, int) to retrieve the values from the model during painting. It is important to remember that the column and row indexes returned by various JTable methods are in terms of the JTable (the view) and are not necessarily the same indexes used by the model.

By default, columns may be rearranged in the JTable so that the view's columns appear in a different order to the columns in the model. This does not affect the implementation of the model at all: when the columns are reordered, the JTable maintains the new order of the columns internally and converts its column indices before querying the model.

So, when writing a TableModel, it is not necessary to listen for column reordering events as the model will be queried in its own coordinate system regardless of what is happening in the view. In the examples area there is a demonstration of a sorting algorithm making use of exactly this technique to interpose yet another coordinate system where the order of the rows is changed, rather than the order of the columns.

Similarly when using the sorting and filtering functionality provided by RowSorter the underlying TableModel does not need to know how to do sorting, rather RowSorter will handle it. Coordinate conversions will be necessary when using the row based methods of JTable with the underlying TableModel. All of JTables row based methods are in terms of the RowSorter, which is not necessarily the same as that of the underlying TableModel. For example, the selection is always in terms of JTable so that when using RowSorter you will need to convert using convertRowIndexToView or convertRowIndexToModel. The following shows how to convert coordinates from JTable to that of the underlying model:

And also, the javadoc for getValueAt:

public Object getValueAt(int row, int column)

Returns the cell value at row and column.

Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering.

However, you can use a table wrapper as described in this other StackOverflow topic. Indeed the method you called doesnt exist on JTable and seems to have come from that topic already! Hope this helps.

2 Comments

Thanks, that's exactly the info I needed. I was looking at the model and the the view. Still having some issues using convertRowIndexToView() when the data is sorted in reverse order. Ill have to look into this. But I will tell you something funny. That other post you linked is for my wrapper to a JTable. I just updated that code to my newest version. But glad people are looking at it, haha.
Thats hilarious! Well glad the information was helpful and yes, its good to see your post if being used already :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.