JComboBoxes and Jave Enumerations : True Love

Introduced in Java 1.5, the native enum class is a great match for using as items in JList, JTable, and as shown below, a JComboBox.  At it's simplest, the enum is nothing more than a type safe collection of identifiers.  Such as:

public enum ColorEnum {
   BLUE,
   BLACK,
   GREEN;
}

It can be used in place of the C style Integer based enums

ColorEnum color = ColorEnum.BLUE;
if (color != ColorEnum.BLACK) {
   throw new IllegalArgumentException("Error: Colors don't match up");
}

The above example is sort of non-sensical, but it gives a simple usage example of Enumerations.  The main advantage is that enums are type safe, you'll never miss a compiler error if you attempt to compare something like the day of the week to the percipitation type.  Enums also help break up the global namespace that can plague applications,  Both of those are good reasons to use Enumerations, but why use them in JComboBoxes?  Because, like classes, they can be extended with new functionality.  Look at the Enumeration below, which I will be utilizing in my examples:

public enum Lense {

    FISHEYE ("Nikon",8, 4.5f, "A favorite of landscape photographers"),
    WIDE_ANGLE ("Nikon", 12, 8.0f, "I've never tried this lense"),
    FAST ("Canon", 135, 2.8f, "Useful for basketball games"),
    TELEPHOTO ("Sony", 200, 4.5f, "Great for the kids");
    
    String brand;
    int focalLength;
    float aperture;
    String note;
    
    Lense(String brand, int focalLength, float aperture, String note) {
        this.focalLength = focalLength;
        this.aperture = aperture;
        this.brand = brand;
        this.note = note;
    }
    
    @Override
    public String toString() {
        return brand + " " + focalLength + "mm / " + aperture + "f";
    }
    
    public String getNote() {
        return note;
    }
}

Notice that along with the individual lense type declarations are 4 parameters.  In this case they represent the lense manufacturer, focal length, aperture, and a general comment about the lense.  Enumerations can have any number of parameters after it. Below the parameters are instance fields to store them, and below that is a non-public constructor, which takes those parameters in the same order declared above.  Throw in a custom toString() method and viola - the perfect class for list population!

I'm not going to show all the UI code, just the relevant portions.

comboBox.removeAllItems();

for(Lense l : Lense.values()) {
   comboBox.addItem(l);
}

Here's the result:

JComboBox with the default selection

The data in the note field is retreived with the following code:

private void comboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    Object obj = comboBox.getSelectedItem();
    if (obj != null) {
        noteField.setText(((Lense)obj).getNote());
    }
}

The toString() method provides the text shown in the JComboBox, and the full selection list is shown below:

JComboBox selection list

The beauty of this setup is there are no wrapper objects to instantiate.  Also, if the list needs to be expanded or modified, a single change to the enum will update every control that uses the above method.  It's simple, effective, and reliable!