• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple selection in jlist  RSS feed

 
aminur rashid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a JList whose data is set by setListData(Object [] ) method.
Now if i have another object [] say arr2 so that i have to select those items and to keep the selection in the JList how to do that.??
I have seen the method JList.setSelectedValue(object, true);
method but then it takes a single object and if i traverse arr2 and select each element separately selection event is fired each time and moreover only the last object remans as selected(obviuous i guess).
So how to select those object in the JList.
Thanks in advance
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first set the list to multiple select mode:
list.setSelectionMode(JList.MULTIPLE_INTERVAL_SELECTION);
There are many methods for setting and adding selections. Unfortunately, they all take indices as parameters, not objects, but I'm sure you can work around that.
 
aminur rashid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer.
My list is selection is already set to MULTIPLE_SELECTION.
I thought of using

but then I was not able to find how i can locate that what is the index of an object. I have an array of object which i have to select.I cant find a method Jlist.getIndexOf(Object) or JListModel.getIndexOf(object) There was a method in DefaultListModel I suppose but then i am not using that model. Any ideas?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!