Java JComboBox with Key=Value pairs

Posted on 2009-12-17
Last Modified: 2013-12-14
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

Question by:amp834
    LVL 37

    Expert Comment

    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"

    Author Comment

    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.
    LVL 37

    Expert Comment

    >> 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

    Author Comment

    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.
    LVL 24

    Accepted Solution


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

    Hope this helps.

        Tomas Helgi

    Author Comment

    Thanks Thomas, this looks like something that will help.  I will look at it today in more detail.

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now