Note that all xxSelectedValue methods are convenience traversal methods around the selectionModel method (which only supports index-based access) in JList. Setting multiple parameters to the same value is not supported. If you really want this, you will have to implement the convenience method yourself. Basically, you will have to iterate over the model elements until you find the corresponding indexes and call the index-based methods, for example:
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; }
source share