Frakcool
Frakcool

Reputation: 11153

JComboBox fill with enum variable value

I have a JComboBox that I made this way, using an enum for its values:

JComboBox<StudyGrade> maxLevelOfStudiesCombo = new JComboBox<StudyGrade>(StudyGrade.values());

The enum looks like this:

public enum StudyGrade {
    ELEMENTARY ("Primaria"),
    MIDDLE ("Secundaria"),
    MIDDLE_HIGH ("Preparatoria"),
    HIGH ("Universidad"),
    MASTERS ("Maestría / Posgrado"),
    DOCTORATE ("Doctorado"),
    POST_DOCTORATE ("Post Doctorado");

    private String studies;

    private StudyGrade(String studies) {
        this.studies = studies;
    }

    public String getStudies() {
        return studies;
    }

    public void setStudies(String studies) {
        this.studies = studies;
    }

    @Override
    public String toString() {
        return studies;
    }
}

As you can see I'm overriding the toString() method, so I can have the studies values shown instead of the enum ones...

However I want to show the studies values only in the JComboBox not everytime I use the StudyGrade enum.

How would I change the code, so whenever I use something like:

System.out.println(StudyGrade.HIGH);

I get printed HIGH instead of Universidad, but not for the JComboBox?

Upvotes: 2

Views: 1612

Answers (3)

Oleg Mikheev
Oleg Mikheev

Reputation: 17444

You're looking to extend an enum, but that's impossible. It means that something is wrong with your requirement.

Rendering is done in the UI component, and it's not enum's business to deal with presentation of data. You should make you UI component render enum the way you'd like instead of trying to make enum understand where it's being used. Since you're a Swing fanatic you should know how to do that, something like this:

maxLevelOfStudiesCombo.setRenderer(new DefaultListCellRenderer() {
    @Override
    public Component getListCellRendererComponent(JList<?> jList, Object o, int i, boolean b, boolean b1) {
        Component rendererComponent = super.getListCellRendererComponent(jList, o, i, b, b1);
        setText(o instanceof StudyGrade ? ((StudyGrade) o).getStudies() : o.toString());
        return rendererComponent;
    }
});

That's going to do that.

Upvotes: 2

Guillaume Mercier
Guillaume Mercier

Reputation: 401

You could just remove the toString override as the default toString for an enum is to return the name of the enum element.

And you could just have a simple for loop that would iterate through the values in your enums and add it to a string array. Then, you would need to pass that array as the argument for your JComboBox and it should be gold.

The code for it should look a bit like that:

//get all study grades
StudyGrade[] temp = StudyGrade.values(); 

//create a string array of same length as the array
String[] str = new String[temp.length];

//append all the studies value to the string array
for(int i = 0; i< temp.length; i++){
  str[i] = temp[i].getStudies();
  System.out.println(temp[i]);//debug
}

System.out.println("---------------------");//debug
for(String s : str){//debug
  System.out.println(s);//debug
}//debug

//pass it
JComboBox<StudyGrade> maxLevelOfStudiesCombo = new JComboBox<StudyGrade>(StudyGrade.values());

Here is an example I made on repl.it

https://repl.it/GH28/1

Upvotes: 0

camickr
camickr

Reputation: 324118

I'm overriding the toString() method, so I can have the studies values shown instead of the enum ones...

I've never used a enum before but I assume you can use it like any custom object added to the combo box so you should be able to use a custom renderer so you can control which data is displayed by the combo box.

Check out Combo Box With Custom Renderer for more information and a helper class.

Upvotes: 1

Related Questions