Java JComboBox with Key=Value pairs

I'm looking to make a simple JComboBox with Key=Value pairs, i.e. InternalValue=DisplayValue.

Example: The combo box options are
S=Save to disk    
G=Group into one file  
N=Don't Save

If the user chooses "Don't Save", the program should get "N" as the current value's short key.

This is similar to the MsAccess combo boxes, and is very useful because we can keep the code the same and change the displayed value without affecting most of the code, which checks for the short code.

I can do this in code, where I make customized ComboBoxRecInfo object, and add the ("S","Save to disk") and other options manually to the combo box.

Is there a simple way to make a class I can use in Netbeans forms, so I can insert a (modified) ComboBox, and specify "S=Save to disk    G=Group into one file   N=Don't Save" at design time.  Or some way to pre-process the "S=xx  G=yy N=zz" string?

In Netbeans, the combo box properties has a "model" field where I can enter the different values the combo box can take.
static class ComboboxRecInfo {

        String name;
        String keyData;

        ComboboxRecInfo(String name, String keyData) {
            setValues(name, keyData);

        public void setName(String name) {
   = name;

        public void setKeyData(String keyData) {
            this.keyData = keyData;

        public void setValues(String name, String keyData) {

        public String toString() {
            return name;

        // find index of item in combo box which matches "name"
        static int findCbKeyDataIndex(javax.swing.JComboBox cb, String keyData, int ValueIfNotFound) {
            for (int i = 0, i2 = cb.getItemCount(); i < i2; ++i) {
                ComboboxRecInfo r = (ComboboxRecInfo) cb.getItemAt(i);
                if (keyData.equals(r.keyData)) {
                    return i;
            return ValueIfNotFound;

Open in new window

Who is Participating?
Tomas Helgi JohannssonConnect With a Mentor Commented:

Here is an example that I think that is what you are looking for

Hope this helps.

    Tomas Helgi
zzynxSoftware engineerCommented:
It's not that difficult.
You can use the String values "S", "G" & "N" as the items in the combo box.
Then you should use a custom listcellrenderer to render (=show, visually represent)
* the "S" as "Save to disk",
* the "G" as "Group into one file" and
* the "N" as "Don't Save"

Cf. paragraph "Providing a Custom Renderer"
amp834Author Commented:
Thanks, zzynx.  Is there a way to specify the entire list S="Save to disk", G="Group into one file", etc.  in the Netbeans form designer somewhere?  I can subclass JComboBox if necessary.  

The other alternative is to write a javabean which can parse the "valid values" property.  I can choose the syntax (e.g. ";S=Save to disk;G=Group into one file;..."), the only mess is I have to compile it separately in another project so I can us it in my GUI project.
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

zzynxSoftware engineerCommented:
>> Is there a way to specify the entire list S="Save to disk", G="Group into one file", etc.  in the Netbeans form designer somewhere?
3 à 4 years ago I switched from NetBeans to Eclipse, so I'm not sure.
But again, I think you should specify the list items as being "S", "G" and "N".
How they are represented visually is completely the responsability of the renderer you use for the combo box.
The "knowledge" that S=Save to disk" and so on should be put into the renderer
amp834Author Commented:
Thanks, zzynx.  I take your response as meaning "I don't know of a way to do it in the netbeans designer", is that correct?

I think of the "renderer" as part of the GUI, not something I want to code separately.  In MsAccess, I could put the entire information when designing the form.  That IS when I typically make the decisions what it looks like on the screen, and what codes I want to use internally to represent them.  A very simple thing.

I will hold the question open a little longer to see if anyone else has ideas how to do it.
amp834Author Commented:
Thanks Thomas, this looks like something that will help.  I will look at it today in more detail.
amp834Author Commented:
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.