• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6815
  • Last Modified:

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) {
            this.name = name;
        }

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

        public void setValues(String name, String keyData) {
            setName(name);
            setKeyData(keyData);
        }

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

0
amp834
Asked:
amp834
  • 4
  • 2
1 Solution
 
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. http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer paragraph "Providing a Custom Renderer"
0
 
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.
0
 
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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.
0
 
Tomas Helgi JohannssonCommented:
          Hi!

Here is an example that I think that is what you are looking for
http://www.java2s.com/Code/Java/Swing-Components/TheKeyedComboBoxmodelallowstodefineaninternalkeythedataelementforeveryentryinthemodel.htm

Hope this helps.

Regards,
    Tomas Helgi
0
 
amp834Author Commented:
Thanks Thomas, this looks like something that will help.  I will look at it today in more detail.
0
 
amp834Author Commented:
thanks!
0

Featured Post

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!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now