bunnytickles
bunnytickles

Reputation: 35

TableRowSorter results in row numbers being disorganized

It is important for my program that the row number assigned to the variable within is consistent with its position, but after sorting this gets messed up. Using DefaultTableModel, this is what I use to sort:

int rownumber = Table.getRowCount();

TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(Table.getModel());

Table.setRowSorter(sorter);

List<RowSorter.SortKey> sortKeys = new ArrayList<>(rownumber);

sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));

sorter.setSortKeys(sortKeys);

The sort is done after pressing a JButton and works as intended visually on the JTable, but the problem is that in reality, the first entry will always be in the position (1,1), seeing as my JTable has two columns. For example, if my table looked like this:

|----|----|
|Col1|Col2|
|----|----|
|  1 |  2 |
|----|----|
|  3 |  4 |
|----|----|

Even though the top row has the values ( 1, 2 ), reading from model.getValueAt(0,1) would return the value 4, as that was the row put in first. I hope this is clear; thanks in advance for any help.

Upvotes: 0

Views: 376

Answers (1)

bunnytickles
bunnytickles

Reputation: 35

Turns out using model.getValueAt() was the problem. Switching to simply Table.getValueAt() resolved the issue.

Upvotes: 0

Related Questions