?
Solved

JComboBox internal value ?

Posted on 2003-03-11
8
Medium Priority
?
229 Views
Last Modified: 2010-03-31
I've a JComboBox, which gets populated from the database with  (say)
employee names. When the user selects a employee, i need to get the employee code of that person.

         Since, employee name is not unique, i can't get the employee
code from the employee name selected.

Now, consider the  following html code

<select>
<option value="E00001">ARRU</option>
<option value="EmpCode">Employee Name</option>
</select>

There can be some internal representation for each value. Is the
same kind of thing possible in JComboBox ?

Thanks in Advance
Arru_in
 
0
Comment
Question by:arru_in
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:arru_in
ID: 8110238
If ur confused with the question, just see the
html code alone & say where same kind of
thing is possible in JComboBox.

Thanks
Arru_in
0
 
LVL 4

Expert Comment

by:thanassis
ID: 8110266
String[] myStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };

//Create the combo box, select item at index 4.
//Indices start at 0, so 4 specifies the pig.
JComboBox myList = new JComboBox(myStrings);
muList.setSelectedIndex(4);
myList.addActionListener(this);
0
 
LVL 2

Accepted Solution

by:
karlika earned 400 total points
ID: 8110268
Sure, just define a simple employee object like:

public class Employee{
  public Employee(String name, String code){
    ....
  }

  public String toString(){
    return employeename;
  }
}

now you can simply add this employee to the JComboBox. Notice that the JComboBox takes Objects instead of trings as argumets.

Whit "(Employee)getItemAt(int pos)" you get the Employee object you chose.

marko
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8110300
And rendering?
0
 

Author Comment

by:arru_in
ID: 8110520
Thanks, everyone for responding..

thanassis, i've have actually used array only. but
i thought there must be a better way (or simple way)
to do it.

 I was expecting a one line answer (any method )
which could serve my purpose. Any how it works.

thanks everyone...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8110555
So if you add

new Employee("Joe Doe", "0001")

what does it say in the combo?
0
 
LVL 2

Expert Comment

by:karlika
ID: 8110807
JComboBox uses the toString() so, in my example combined with CEHJs data it should say "Joe Doe" in the JComboBox

Marko
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8110819
OK ;-)
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question