How to set multiple items as selected in JList using setSelectedValue?

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.

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.

1 that's simply wrong - addSelectionInterval is the method intended to .. well .. adding something to the selection. The exact outcome depends on the SelectionMode – kleopatra May 11 at 8:49 Thanks! ListSelectionModel did the work – Niranjani S May 11 at 9:09.

1 spot-on with addSelectionInterval, but in order for this to be a truly useful answer, an explanation is required. – Крыѝa Dec 9 at 23:41.

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 GetElementAt(i))) return i; } return -1; }.

1 addSelectionInterval does the trick. This should be the accepted accept! :D – КрыÑ?

A Dec 9 at 23:40.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions