• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox - rookie question  RSS feed

 
Jay Faluk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
this rules! thank you!
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to be of help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!