Adding ID to JComboBox

Hello there,

I´ve got a problem with the JComboBox! I get a List of items, which I add to a JComboBox but I´m only able to add the "TEXT" to it. But I also have to add a ID, which identifies the Text, to the ComboBox. Do you have any Solution for my problem??

Thanks a lot for your help!

Greetings
Markus
mfAudiAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Just add instances of this class to your model:

public class MyData {
      private int id;
      private String text;

      public void setId(int id) {
            this.id = id;
      }

      public int getId() {
            return id;
      }

      public void setText(String text) {
            this.text = text;
      }

      public String getText() {
            return text;
      }
      
      public String toString() {
            return text;
      }

}
0
 
CEHJCommented:
The ComboBoxModel holds Object. You can make an object with those fields. Make its toString retrun what you want and it#'ll be displayed
0
 
mfAudiAuthor Commented:
Can you show me a little Example??

THX
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
0
 
CEHJCommented:
mfAudi can you tell me why my answers were ignored?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
This should have been a split.
0
 
mfAudiAuthor Commented:
I don´t know! I accepted your last answer! And I also rated it as "GOOD"!

Why are you asking??

0
 
Mayank SAssociate Director - Product EngineeringCommented:
You accepted my answer, not CEHJ's.
0
 
CEHJCommented:
>>I accepted your last answer!

No you didn't - you accepted *someone else's* answer ;-)
0
 
mfAudiAuthor Commented:
I´m sorry, so did I! But both answers were similar in quality!

Sorry CEHJ! Next time. I was to much in a hurry cause I have to solve a problem at my workingplace!

Have a nice WE!

CU
0
 
CEHJCommented:
>>But both answers were similar in quality!

I have to beg to differ and that's no disrespect to mayank. His link, while useful, does not deal directly with your specific problem
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Ah, so CEHJ takes the name finally in his 9 years at EE ;-)

mfAudi, if the sample code on the link which I posted helped you and if that was the one you used, then you should split the points between CEHJ and me because he had also posted a similar code earlier.

I'll ask Venabili to re-open.
0
 
CEHJCommented:
:-)
0
 
mfAudiAuthor Commented:
OK, no Problem! Both of you helped me very much!

But no I finisch working and I will do it on monday!

Hasta la vista!

Greetings from good old Germany! (SOCCER-World-Championship-2006) ;-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
:-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Both of you helped me very much!

mfAudi, you can now follow the Split Points link which you see below (and above the comment box) to split points between CEHJ and me.
0
 
objectsCommented:
> Greetings from good old Germany! (SOCCER-World-Championship-2006) ;-)

Go the socceroos :)
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.