in my GUI i have the
@Override
public void actionPerformed(ActionEvent ae) {
state = new JComboBox(EnumStates.values());
state =(JComboBox)ae.getSource()
state.getSelectedItem() //this returns what I want
then I have object of some other class like that uses the EnumStates
CallmeClass obj;
and when I try to set the state of the enum with the result of the JComboBox like this
obj.setState(state.getSelectedItem());
I get compile error
1. required state but found object
So my question is is there a way to make the setState take as argument state.getSelectedItem() withouth changing the return type of the method setState() or re declaring the enums in the gui. Thanks.
I’m guessing your declaration for
setStateis seomthing like this:The problem is that JComboBox is untyped (at least it was that way until Java7). So
getSelectedItem()always returns an object that needs to be casted to your type. So you can do the cast when you’re getting the item:Or you can change your method declaration to object and do your cast there: