I am working on DefaultListModel
. I would require to move the selected item to first or to last(not up and down) location in list. How to move the selected item in a DefaultListModel
at first or last location without swapping?
I'm guessing that the problem you're having is that when you hit your "up" or "down" button, the item moves, but because you removed it from the list and then added it again, it is no longer selected. The list selection model processes the "remove" event and clears its selection because the selected item was removed.
I know of two ways to fix this, both inelegant:
I've generally used (2), because although it is a bit more work, it allows the moving logic to deal only with the list model and not the selection model.
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.