• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting JList Selections from String[]

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Scott Florez
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic