I want to add different Enums to a single JComboBox. Here is how the code looks like. Type contains 3 different type of Enums(Colors, Shapes, Dimensions).

final JComboBox typeJComboBox = new JComboBox(Type.Colors.values());
for(Type.Shapes shape: Type.Shapes.values()) {
typeJComboBox .addItem(shape);
}
for(Type.Dimensions dimension : Type.Dimensions.values()) {
typeJComboBox .addItem(dimension );
}

What generic type do I use? When using eclipse, there is a yellow squiggly line under JComboBox and when you hover your mouse over it, it says "Infer Generic Type Arguments...".

Recommended Answers

All 14 Replies

Why not just Enum (This is the common base class of all Java language enumeration types)? No need for generics.

@JamesCherrill this question is about (my wild shot to the dark) painting shapes in ListCellRenderer and reference from Action/ItemListener back via array to Enum's ID, nothing compilcated (my view), answered milion:-) times, but I'm not going answering an question without any question inside here, there, nor to question from empty space cca 3gradient left from Milky Way

Yes, you may well be right. I was just answering the immediate question about how to declare the type for the combo box. But yes, the question "why?" is probably the one we should be asking.

Declaring the type as Enum does not work. I've attempted this already.

Yes it does - you must be making some other mistake - try this code:

    enum E1 {A,B,C}
    enum E2 {E,F,G}

    ...

        JComboBox <Enum> e = new JComboBox <>();
        e.addItem(E1.A);
        e.addItem(E2.E);

I have it as...

JComboBox <Enum> e = new JComboBox <>();
for(Type.Colors c : Type.Colors.values()) {
    e.addItem(c);
}
for(Type.Shapes s : Type.Shapes.values()) {
    e.addItem(s);
}
for(Type.Dimensions d : Type.Dimensions.values()) {
    e.addItem(d);
}

There is still a warning (yellow squiggly line) under <Enum>. I hover over it and it still says "Infer Generic Type Arguments..." and Add @Suppress...

Bizarre... no such warning in NetBeans

Well there is one on Eclipse.

This looks like an Eclipse problem? ... if you allow Eclipse to infer generic arguments it dos nothing - select "preview" for the fix and it says "the refactoring does not change any source code".
Anyway, you can avoid the error message by giving it a (spurious) wild card, as in
JComboBox <Enum<?>> e = new JComboBox <>();

That seems to solved the Eclipse warning issue.

Any idea how I can access the Enum methods? The class Type contains 3 Enums (Colors, Shapes, Dimensions) and each Enum contains methods like getString(), etc.

hmmm for why hells on this world you killing something clear, simple, blablablab :-)

  • can't add enum, even is possible but have some side effect inside/as underlaying model for concrete JComponents XxxModel, then you would need to enlarge additional logics on XxxModel side, btw then enum is useless as container for constants, don't to use enum, add Objects directly to JComponents XxxModel

  • you don't need to add enum,

  • you need to override returns somthing in enum, value in String, Object, Double, Integer... depends of XxxModel for JComponent

code

    public enum DaysOfTheWeek {

        MONDAY("MondayXxx", "MON", "First day of the work week."), TUESDAY("Tuesday", "TUE", "Second day of the work week");
        // etc ...
        private final String fullName;
        private final String abbrvName;
        private final String description;
        private final int noFromDays;
        private final boolean isWeekend;
        private final boolean isHollyday;
        // etc ...

        private DaysOfTheWeek(String fullName, String abbrvName, String description) {
            this.fullName = fullName;
            this.abbrvName = abbrvName;
            this.description = description;
        }

        public String getFullName() {
            return fullName;
        }

        public String getAbbrvName() {
            return abbrvName;
        }

        public String getDescription() {
            return description;
        }

        public int noFromDays {
        }

        public boolean isWeekend {
        }

        public boolean isHollyday {
        }

        // etc ... see there 5-11 another additional params
    }

.

  • then loop inside this enum, add Objects from enum (and then another enums)

  • inside XxxRenderer

      if (value instanceof XxxXxx && Xxx.getUserObject() instanceof DaysOfTheWeek){
          ((JLabel) cell).setText(((DaysOfTheWeek) Xxx.getUserObject()).getFullName());
      }
    

.

dot

Any idea how I can access the Enum methods? The class Type contains 3 Enums (Colors, Shapes, Dimensions) and each Enum contains methods like getString(), etc.

You'll need to test the selected item to see which enum it belongs to, then cast to the appropriate type (colors, Shapes etc), then you can call all its methods.

(ps: not sure why mKorbel objects so strongly - personally I have no problem with using enums in a combo box, although I think mixing 3 different types of enum in one box may suggest an odd design choice?)

Continuing the fun here: If your three enums have the same methods then you can define your enums as implementing an interface that defines those methods, eg

    interface IFace {
        String getString();
    }

    enum E1 implements IFace {
        A, B, C;
        public String getString() {
            return "E1." + name()
        }
    }

    enum E2 implements IFace {
        E, F, G;
        public String getString() {
            return "E2." + name()
        }
    }

Now you can define your combo box in terms of that interface, add any members of any enum that implements the interface, and directly call the interface's methods on the combobox items without any casting etc...

        JComboBox<IFace> e = new JComboBox<>();
        e.addItem(E1.A);
        e.addItem(E2.E);
        ...
        System.out.println(e.getItemAt(0).getString()); // works
        System.out.println(e.getItemAt(1).getString()); // works
commented: :) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.