简体   繁体   中英

Sorting of JCombo Box's Not working while sorting Jtable Column

When a sorter/autoRowSorter added to JTable that contains with different JCombo Box in the table cell is sorted with column header, Jcombo Box's are not sorted with respective to row.

JCombo Box is clicked to change the data it ended up in showing Model values of combo box rather than view values(Row related Values).

Example found on internet, using the same to illustrate the problem. I have just added setAutoCreateRowSorter(true)

Reference of Code:

how to add different JComboBox items in a Column of a JTable in Swing -- Example is in answer.

Snapshots:

After Sorting the data seems to be changed with respective to view:

http://i.stack.imgur.com/NRiZB.png

But when Combo Box is selected for options, Name Drop-down is shown when we expect to have color:

http://i.stack.imgur.com/74WIw.png

I do understand that mapping with respective to view is not done correctly when the sorting is done.

I do understand that mapping with respective to view is not done correctly when the sorting is done

And that is what you need to fix.

Basically you can no longer access the editor based on the row of the table. Instead you need to convert the table row to the model row.

int modelRow = table.convertRowIndexToModel(tableRow);

Then you access the editor map by using the modelRow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM