Solved

Adding ID to JComboBox

Posted on 2006-06-23
19
442 Views
Last Modified: 2008-03-03
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
0
Comment
Question by:mfAudi
  • 7
  • 6
  • 4
  • +1
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 

Author Comment

by:mfAudi
Comment Utility
Can you show me a little Example??

THX
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 65 total points
Comment Utility
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
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 60 total points
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
mfAudi can you tell me why my answers were ignored?
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
This should have been a split.
0
 

Author Comment

by:mfAudi
Comment Utility
I don´t know! I accepted your last answer! And I also rated it as "GOOD"!

Why are you asking??

0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
You accepted my answer, not CEHJ's.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>I accepted your last answer!

No you didn't - you accepted *someone else's* answer ;-)
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:mfAudi
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0
 

Author Comment

by:mfAudi
Comment Utility
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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
:-)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> Greetings from good old Germany! (SOCCER-World-Championship-2006) ;-)

Go the socceroos :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now