aspose file tools
The moose likes Swing / AWT / SWT and the fly likes JComboBox  - rookie question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "JComboBox  - rookie question" Watch "JComboBox  - rookie question" New topic
Author

JComboBox - rookie question

Jay Faluk
Greenhorn

Joined: Apr 25, 2002
Posts: 9
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

Joined: May 05, 2000
Posts: 13974
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:


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jay Faluk
Greenhorn

Joined: Apr 25, 2002
Posts: 9
this rules! thank you!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Glad to be of help!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: JComboBox - rookie question
 
Similar Threads
updating check box with HQL in Struts 1.3
how to store datecombobox in an arraylist of type combobox
Making arraylist from resultset
xsd : defining array
Collection