I am working on a viewer, which uses a JList to show thumbnails of the pages of a document. The user can open a page by selecting it through in the JList, or throught other mechanisms, like entering the number in a text box.
When using the latter alternative, I want that the JList also selects the page. I do this using setSelectedIndex(), but this triggers an event, which causes the page to be loaded again, as if the user had clicked and selected the page in the JList, and this is causing me some problems.
How I see it, the index should be set some way (perhaps in the model) so that only the UI of the JList updates, without firing an event that the index has changed.
Is this possible? Or is there a better way to solve my issue?
You can remove all
ListSelectionListenerfrom the list, make a selection and then add them again.You can create your own
ListSelectionModelwith a method that doesn’t throw the event and set it as a selection model to yourJList, and then usegetSelectionModel().yourSelectIndexMethod(index).You can also divert all your other methods of selection to the list, just find the corresponding entry if selecting the page by other means and select the item in the list. This way the item is selected and the page is loaded once.
Code for option 2: