This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Setting JList Selections from String[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Setting JList Selections from String[]" Watch "Setting JList Selections from String[]" New topic
Author

Setting JList Selections from String[]

Scott Florez
Ranch Hand

Joined: Dec 05, 2006
Posts: 58
Hi. I have a String[] of possible values for a JList, and I want those values to be highlighted on the JList. Here's what I've tried:



It compiles, but the entries aren't getting properly highlighted. The delimiter for the values in the database is two spaces. Thanks for the help!
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
setSelectedValue() clears any previous selection. To set them all at once look into setSelectedIndices() or to add them one by one try getSelectionModel().addSelectionInterval(pos, pos). Either way you have to convert the value into an index.


bitguru blog
Scott Florez
Ranch Hand

Joined: Dec 05, 2006
Posts: 58
Originally posted by Brian Cole:
setSelectedValue() clears any previous selection. To set them all at once look into setSelectedIndices() or to add them one by one try getSelectionModel().addSelectionInterval(pos, pos). Either way you have to convert the value into an index.


Thanks for the information, Brian. I did find that my code is highlighting only the last element of the array. Unfortunately, the JList's content is dynamic. The possible options for the JList are actually read from another database, and more values are constantly added. That seems like it would make using index values very dicey. Is there some other way I can do it and still allow the possible JList entries to be dynamic?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
maybe(?) something like this

when it opens, 'c' should be selected.
click the button, and a random letter will be added to the JList.
if the random letter is in the checkList it will (should) also be selected.

looks messy, feels messy, perhaps someone has a simpler solution.

Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Scott Florez:
the JList's content is dynamic. The possible options for the JList are actually read from another database, and more values are constantly added. That seems like it would make using index values very dicey.


Well if you look at how setSelectedValue() is implemented, you'll see that it looks up the index for the value you passed in and calls setSelectedIndex().

Having to do it yourself is more work for you, but I don't see how it would be any more dicey.
[ January 16, 2007: Message edited by: Brian Cole ]
Scott Florez
Ranch Hand

Joined: Dec 05, 2006
Posts: 58
Originally posted by Brian Cole:


Well if you look at how setSelectedValue() is implemented, you'll see that it looks up the index for the value you passed in and calls setSelectedIndex().

Having to do it yourself is more work for you, but I don't see how it would be any more dicey.

[ January 16, 2007: Message edited by: Brian Cole ]


Well I ended up using index values and seemed to get it working OK. The problem with that is that it now looks messy, plus requires me to re-code my application whenever new possible JList entries are added. I was hoping that the application wouldn't have to be modified when people added new entries to the database. Oh well. I appreciate the help everyone. Thanks a lot!

Below is the code I ended up with:


[ January 17, 2007: Message edited by: Scott Florez ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Scott Florez:
Well I ended up using index values and seemed to get it working OK. The problem with that is that it now looks messy, plus requires me to re-code my application whenever new possible JList entries are added. I was hoping that the application wouldn't have to be modified


I don't see why you would have to hard-code the indices. Can't you just dynamically iterate through the ListModel and detect which indices you want to select? That's pretty much what the setSelectedValue() method does.
 
jQuery in Action, 2nd edition
 
subject: Setting JList Selections from String[]
 
Similar Threads
Vector or ArrayList use for dropdown with more than 1000 entries
JList model elements pre exist, updating of vector does not change model
Single interval selection in two lists
not able to get the items from JList
comparing a string value with a jtable value