• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to highlight previously selected JList elements

 
Russell West
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am developing a Java Swing application that allows a user to display a JFrame which contains various JButtons that allow operations such as Add, Delete, Update, Print actions to be performed on a JList. The user can also select one or more of the current JList elements with the selected elements not having to be consecutive. For instance the user could select the first element and last element with elements between not selected. What I want to be able to do is allow the user to select whatever elements so desired, and also allow them to add additional items to the end of the JList. Once such operation is completed I want to highlight again the previously selected elements. My code saves off the selected indices of the JList in an int array prior to the add operation taking place. For the life of me I can not figure out the code syntax to allow me to iterate through the saved off indices and highlight/select the corresponding JList elements. I have tried numerous available methods for a ListSelectionModel such as setSelectionInterval, but have only been able to highlight one of the previously selected elements. Any assistance that can be provided is always greatly appreciated.


 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, the API documentation for JList says

By default, JList's selection model is configured to allow any combination of items to be selected at a time; selection mode MULTIPLE_INTERVAL_SELECTION.


So that part is easy -- that's what you want. Looks like you're okay with that part anyway.

Now you say you have a list of the indices of the JList which were previously selected, and you don't say that somehow they have become deselected but apparently that's the case. To reselect those indices you just pass that array to the JList's setSelectedIndices() method. There's no need to loop through the array and select the indices individually.

I don't really understand why the selected indices become deselected (as I mentioned). Why don't you just leave them selected as the user adds more entries to the list, if you want them to be selected?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch!

(I notice you posted this in the Ranch Office forum, which is supposed to be for discussing how the Ranch works and how it could be made better. We have a Swing etc. forum where this post should go, so I'll just move it over there.)
 
Fred Kleinschmidt
Bartender
Posts: 459
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried ?
 
Russell West
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The recommended method call setSelectedIndices( indices ) provided by Fred Kleinschmidt worked. Thanks for the help.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The recommended method call setSelectedIndices( indices ) provided by Fred Kleinschmidt worked.


Actually, Paul suggested that yesterday.
 
Russell West
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected Paul was the first forum member to suggest the correct fix. Want to give credit where credit is due.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!