• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

JComboBox - rookie question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing my first swing pgm, so this may sound ridiculous, but here it is --
i have an employee table, employee number and name being the fields. i want to display the names in a jcombobox, but do the maintenance (edit/create/delete) using the employee number. how do i do this? i.e if the user selects the name, how do i know the corresponding number? is there a standard way of doing this? i have read material, but need to know what the gurus say ;-)
thanks to all in advance.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The beauty of Swing over AWT. JComboBox takes an object as an item to add to the box. It will run the toString() method of that object to display the object. But when you run the getSelectedItem() you will get back the object. So the trick is to create a class that has all the fields you want (let's say the name and number). Add a toString() method that displays the name. When you get the object back, you can grab the number.

Create a bunch of these and either (1) add them one at a time to the JComboBox or (2) put them in an array and pass the array into the constructor of the JComboBox or (3) stick them in a Vector and pass the Vector into the constructor of the JComboBox.

Now when the user clicks on the JComboBox you do this:

 
Jay Faluk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this rules! thank you!
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to be of help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic