Sort JTable rows manually and with TableRowSorter at the same time

I need to create a table that can be sorted by clicking the table header AND, translating individual and several rows manually.

I made a few buttons to move the selected rows in the table model up, down, up, or down. These buttons modify the table model and then update the JTable. That alone works great.

But when I start sorting the rows by clicking on the columns in the table header, everything goes wrong. Manual sorting works with the collection in the table model, sorting by clicking on the header works with some kind of table view.

Is there a way to move rows manually only as a table, and not in a collection of table models? Or is there another better solution?

+3
source share
1 answer

JTableThe API refers to the relationship between model and view coordinates regarding sorting. In particular, it says: "In the examples area, there is a demonstration of a sorting algorithm that uses this particular method to insert another coordinate system where the order of the rows changes, rather than the order of the columns." You can compare what you are doing with the corresponding example in Sort and filter .

+2
source

Source: https://habr.com/ru/post/1774934/


All Articles