Java 想要设置在由枚举填充的 JComboBox 中选择的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20651149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:21:05  来源:igfitidea点击:

Want to set default value selected in JComboBox populated by enum

javaswingenumsjcombobox

提问by astack

Below statement in if condition is not working, please provide me some solution that how to set selected item for JComboBoxwhich are populated by ENUM.

如果条件不起作用,请提供一些解决方案,说明如何设置JComboBox由 ENUM 填充的所选项目。

      public enum EbayContryEnum 
        {
        AT    (3),
        AU    (4),
        BE    (5),
        CA    (7),
        CH    (14),
        DE    (11),
        ES    (13),
        FR    (10),
        IE    (2),
        IT    (12),
        NL    (16),
        UK    (15),
        US    (1);
        }

for ex:-

例如:-

if(country.equals("FR"))
                      {
                      cbImportCountry.setSelectedItem("FR");
                      }

But it's not working..

但它不起作用..

采纳答案by Sanjay Nagare

cbImportCountry.setSelectedItem(EbayContryEnum.FR);

cbImportCountry.setSelectedItem(EbayContryEnum.FR);

回答by camickr

cbImportCountry.setSelectedItem(EbayContryEnum.FR);

回答by fo0

Maybe you are not looking for assign the value to the combobox. You are looking for casting your value to the enum.

也许您不是在寻找将值分配给组合框。您正在寻找将您的价值转换为枚举。

You can do this via:

您可以通过以下方式执行此操作:

EbayContryEnum.valueOf("FR");