How to set multiple items as selected in JList using setSelectedValue?
Solution 1
Although StanislasV answer is perfectly valid, i would prefer to avoid adding one selection interval to another. Instead, you should prefer to call the JList
associated ListSelectionModel#setSelectionInterval(int, int)
method like this :
jList.getSelectionModel().setSelectionInterval(0, 3)
If you want your list selection to be disjoint, you'll moreover have to write your own ListSelectionModel
.
Solution 2
Note that all xxSelectedValue methods are convenience wrapper methods around the selectionModel (which supports index-based selection access only) on the JList. Setting multiple selections per value is not supported. If you really want it, you'll have to implement a convenience method yourself. Basically, you'll have to loop over the model's elements until you find the corresponding indices and call the index-based methods, something like:
public void setSelectedValues(JList list, Object... values) {
list.clearSelection();
for (Object value : values) {
int index = getIndex(list.getModel(), value);
if (index >=0) {
list.addSelectionInterval(index, index);
}
}
list.ensureIndexIsVisible(list.getSelectedIndex());
}
public int getIndex(ListModel model, Object value) {
if (value == null) return -1;
if (model instanceof DefaultListModel) {
return ((DefaultListModel) model).indexOf(value);
}
for (int i = 0; i < model.getSize(); i++) {
if (value.equals(model.getElementAt(i))) return i;
}
return -1;
}
Solution 3
jList.addSelectionInterval(0,2);
Niranjani S
Updated on June 26, 2022Comments
-
Niranjani S almost 2 years
I have a jList that is populated dynamically by adding to the underlying listModel. Now if I have three Strings whose value I know and I do
for(i=0;i<3;i++){ jList.setSelectedValue(obj[i],true);//true is for shouldScroll or not }
only the last item appears to be selected...If this can't be done and I have to set the selection from the underlying model how should I go about it???
Also please note the jList has selection mode:
jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
Thanks in Advance
-
kleopatra about 13 years-1 that's simply wrong - addSelectionInterval is the method intended to .. well .. adding something to the selection. The exact outcome depends on the SelectionMode
-
mre over 12 years+1
addSelectionInterval
does the trick. This should be the accepted accept!!!! :D -
mre over 12 years+1 spot-on with
addSelectionInterval
, but in order for this to be a truly useful answer, an explanation is required. -
PhoneixS over 9 yearsGet in mind that DefaultListModel.indexOf(value) already do a linear search so it's the same using indexOf than the for version (the same efficiency although more readable).
-
kleopatra over 9 years@PhoneixS it's an implementation detail that we can't know :)
-
PhoneixS over 9 yearsWe can really know, at least with oracle and OpenJDK as we can see the source code (like I have done). It's the good thing about OpenSource :D
-
kleopatra over 9 years@PhoneixS yeah, we could read - but we can't know for sure because the implementation isn't part of the specification and could change without notice :) Don't rely on implementation details, never-ever ...
-
PhoneixS over 9 years@kleopatra I totally agree, but it's something to get in mind so you don't think that
indexOf
is somewhat magic.