How to set up multiple items in JList using setselectedvalue?
•
Java
I have a JList that is dynamically populated by adding to the underlying listmodel Now, if I have three strings, I know and I know its value
for(i=0;i<3;i++){ jList.setSelectedValue(obj[i],true);//true is for shouldScroll or not }
Only the last item seems to be selected... If this cannot be done, I must choose from the basic model settings. How should I do it?
Also note that JList has a selection mode:
jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
Thank you in advance
Solution
Although stanislasv answer works perfectly, I'd rather avoid adding one selection interval to another Instead, you should prefer to call the listselectionmodel#setselectioninterval (int, int) method associated with JList like this:
jList.getSelectionModel().setSelectionInterval(0,3)
If you want list selections to be disjoint, you must also write your own listselectionmodel
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码