• Post Reply Bookmark Topic Watch Topic
  • New Topic

JListbox  RSS feed

 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having difficulty understanding the JListBox, hoping someone might be able to help out..

I'm populating the listbox via a resultset, looping through the resultset creating an array of strings to add to the listbox.

Thing is I dont just want to add the strings to the listbox I also want to add the unique id of each record in my resultset, so I need a multi-column listbox and the ability to hide a particular column. Can anyone tell me if this is possible without a whole load of extra code.

Thanks,
Dave.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like what you wish for is like an HTML SELECT Element with the values hidden and the text visible. To get something like this in Swing, I would make objects out of the results of your query and add an array of the objects to the JList. The JList converts any object you put in there to a String using the object's toString method for display purposes. You can override the toString method so it shows what you want. Then, when you take the object back out with getSelectedItem(), it has all the data you originally put in there.

For me, overriding toString() became a problem with objects that are used for a bunch of different things. If this is the case, you use jList.setRenderer() to set a Renderer for your list.
ListCellRenderer
 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim, that worked a treat.

Regards,

Dave.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!