• 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

Please Help: JList selectable options from 'name' field from each object in a HashMap

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there.
i have three classes...
HolderFrame
Company
Employee
... HolderFrame has an instance of the Company object... Company has a HashMap of Employee objects, and has a getHashMap() method which returns the Collection of Employee objects...
so ... the JList is in the HolderFrame. I need to construct it, and place it, and i am not sure how to pull in first_name field from each Employee objet within the HashMap (provided by the Company class, which has gathered up a whole lot of Employee objects by reading in fields from a local txt file).
Please help.
[note - i am not sure if this should be in advanced or medium java or swing forum, but as it is dealing with a JList, i assume Swing].
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do something like the following:
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you could also do the following:
Create an array of employee objects from the collection of employees.
Write a custom ListCellRenderer that extracts the display string from the object and passes it to the default superclass' renderer. Call JList.setCellRenderer and pass it an instance of your renderer.
The nice thing about this is that, if I'm not mistaken, getSelectedValue() will return the employee object itself; there is no need to look up the object by the displayed name.
- Keith
 
Jasper Vader
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
brilliant stuff, i am going to have a go at implementing these ideas immeeeediately!
i have been away from the swing stuff for a bit, as i had to work on some oracle stuff, but now must develop the swing schwing.
so thanks for the replies, will let you know how i get on this afternoon.
 
Jasper Vader
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nate - I have not been able to get your example working yet unfortunately.
Keith - an interesting idea, am willing to try it, but i sort of dont have a concept of the implementation of it yet.
I am actually in need of a bit of practical help here. I presume bartender Nate's advice was right on the money, but it is not integrating with the code i am trying to work it into (could be the way i put it in, where i put it, or the other classes)... oh well, will keep on trying ... reading .. typing ... compiling .. running ...
cool hit me back if any good ideas to illuminate me occur anyone please.
[ June 02, 2003: Message edited by: Jasper Vader ]
 
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic