JComboBox填充枚举变量值 [英] JComboBox fill with enum variable value

查看:172
本文介绍了JComboBox填充枚举变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JComboBox ,我以这种方式,使用枚举作为其值:

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

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

枚举如下所示: p>

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;
    }
}

正如你所看到的,我覆盖了 toString()方法,所以我可以使用研究显示,而不是枚举 some ...

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

但是,我只想在<$ c中显示研究 $ c> JComboBox 不是每次我使用 StudyGrade 枚举。

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);

我打印 HIGH 而不是 Universidad ,但不适用于 JComboBox

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

推荐答案


我覆盖了toString()方法,所以我可以显示研究值,而不是枚举...

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.

这篇关于JComboBox填充枚举变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆