0

I'm trying to populate JComboBox with enums declared in Colour.java. I can access the description of the enums using Colour.values() but is it possible to access the enum declaration itself? I'd like the JComboBox populated with Blue and Red. I've had a look at http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html to no avail.

package example;
import javax.swing.*;

public class ColourView extends View {

private JLabel colourLabel;
private JComboBox comboBox;
private DefaultComboBoxModel model;

public ColourView() {
    colourLabel = new JLabel();
    colourLabel.setText("Colours");
    colourLabel.setBounds(20, 30, 70, 20);
    mainContentLayeredPane.add(colourLabel, JLayeredPane.DEFAULT_LAYER);

    comboBox = new JComboBox(Colour.values());     

    comboBox.setSize(100, 20);
    mainContentLayeredPane.add(comboBox, JLayeredPane.DEFAULT_LAYER);
}

public void setComboBox(String[] list) {
    model = new DefaultComboBoxModel(list);
    comboBox.setModel(model);
}
}

package example;

public enum Colour {
BLUE("Blue Paint", 12),
RED("Red Paint", 4);
private String description;
private int value; 

Colour(String description, int value){
    this.description = description; 
    this.value = value; 
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getValue() {
    return value;
}

@Override
public String toString() {
    return description;
}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

2

If you want a little sample of the same color to show up next to the color name, you'll need a custom renderer. If you start with a DefaultListCellRenderer, you can add an Icon that's painted with the same color, or change the background like they show here.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42