简体   繁体   中英

Java: JComboBox won't accept enum.values()

So I'm working on a practice program where I'm trying to use an Enum in a jcombobox. I believe this is a good decision for my program because the options in the combo box are constants, and they also have alternate values assigned to them (a string name in addition to the constant value), so I felt that enum was the best way to go.

Unfortunately, I can't get the combo box to accept the full list of values from my enum constant. I'm pasting my full code below. What am I doing wrong here?

public enum CurrencyTypes{

    USD ("US Dollars"),
    BPS ("British Pound Sterling"),
    E ("European Euros"),
    RR ("Russian Rubles"),
    JY ("Japanese Yen"),
    CY ("Chinese Yuan"),
    IR ("Indian Rupees"),
    NIS ("New Israeli Shekels");

    private String typeName;


    private CurrencyTypes(String typeName){
        this.typeName = typeName;
    }

    public String getTypeName(){
        return typeName;
    }

}

In the Driver class below, the program is failing when I try and initialize currencyBox with the list of values from CurrencyTypes.values(). It compiles fine, but when I run the program I get java.lang.ExceptionInInitializerError and it crashes.

public class AccountDriver{

    private String[] stringArray = new String[] { "", "test1", "test2", "test3" };
    private JComboBox<String> stringBox;
    private JComboBox<CurrencyTypes> currencyBox;
    private JLabel stringSelection;
    private String stringResult;
    private JLabel etSelection;
    private CurrencyTypes[] currencyArray;
    private ArrayList<CurrencyTypes> currencyArrayL;


    public AccountDriver(){


        JFrame testFrame = new JFrame();
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        stringBox = new JComboBox<String>(stringArray);
        stringBox.addItemListener(new StringBoxListener());


        stringSelection = new JLabel(stringResult);


        currencyBox = new JComboBox<CurrencyTypes>(CurrencyTypes.values());


        testFrame.setLayout(new GridLayout(2, 2));
        testFrame.add(stringBox);
        testFrame.add(stringSelection);
        testFrame.add(currencyBox);

        testFrame.setVisible(true);
        testFrame.pack();
    }

    public static void main(String[] args){
        new AccountDriver();
    }

    private class StringBoxListener implements ItemListener{
        public void itemStateChanged(ItemEvent e){
            stringResult = String.valueOf(stringBox.getSelectedItem());
            System.out.println(stringResult);
            stringSelection.setText(stringResult);
        }
    }

    private class CurrencyBoxListener implements ItemListener{
        public void itemStateChanged(ItemEvent e){

        }
    }

}

I don't know anything about ENUMs but... A combo box render requires a toString() implementation to display the values in the combo box. Maybe this needs to be implemented in the enum?

and they also have alternate values assigned to them (a string name in addition to the constant value),

You can also create a custom POJO with the two values to dislay in the combo box. See Combo Box With Custom Renderer for a simple example.

尝试:

new JComboBox(CurrencyTypes.values());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM