posted 25 years ago
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: