LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to add numbers in JComboBox (https://www.linuxquestions.org/questions/programming-9/how-to-add-numbers-in-jcombobox-706371/)

vibinlakshman 02-21-2009 03:01 AM

How to add numbers in JComboBox
 
I will show a small snippet what i did :

1. JComboBox petList = new JComboBox();
2. petList.addItem(1);// also tried petList.addItem(new Integer(1));

When i tried to enter string in combo box its ok , working fine , but when tried to enter numbers its bit problem ..

Error shown ...

Error:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

paulsm4 02-21-2009 02:10 PM

Hi -

You can always use "String.valueOf()":
Quote:

String s = String.valueOf(i);
petList.addItem(s);
Even easier, you can probably just concatenate an empty string:
Quote:

petList.addItem("" + i);
'Hope that helps .. PSM


All times are GMT -5. The time now is 06:59 AM.