Link to home
Start Free TrialLog in
Avatar of gingermeatboy
gingermeatboy

asked on

Swing combobox question

how can you get an int value from a combo box?

I tried this:

final JComboBox ansBox= new JComboBox();
     
     // populate the combo box
     for (int i=1; i<=10; i++) {
         ansBox.addItem(""+i);
     }
     ansBox.setSelectedIndex(0);

then when trying to get the value back out I did:

int number = ((Integer) ansBox.getSelectedItem()).intValue();
.....

but it complains, how do you do it properly?!

cheers              
Avatar of gingermeatboy
gingermeatboy

ASKER

I've changed it so for the above example I just put

int number = (ansBox.getSelectedIndex()+1);

but is there a better way?
ASKER CERTIFIED SOLUTION
Avatar of allahabad
allahabad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks! :o)
You are welcome.