Just want to insert 1-10 values in a ComboBox. How to convert int i to string values?
for(int i=1;i<11;i++){
quantityCombo.addItem(i); //Not accepting int values
}
Just want to insert 1-10 values in a ComboBox. How to convert int i to string values?
for(int i=1;i<11;i++){
quantityCombo.addItem(i); //Not accepting int values
}
Use Generics and Try Like this....
JComboBox<Integer> quantityCombo= new JComboBox<Integer>();
// add items to the combo box
for(int i=1;i<11;i++){
quantityCombo.addItem(i);
}
You can use Integer.toString(i)
to convert your integer into a String.
Your code will look like this:
for(int i=1;i<11;i++){
quantityCombo.addItem(Integer.toString(i));
}
When in doubt, read javadoc.
You are looking for Integer.parseInt(int), or toString(int). Plenty of choices.
And the real way of solving this is to give a model to the combo box. You don't want to add its "items" one by one; you create a data structure (for example an array) that you pass as one thing; see the official tutorial.
Finally: ComboBoxes accept a type parameter; so as khaja suggested, you can also create a ComboBox that will display only Integer entries. And when an Integer is expected, the compiler will do the work for your, and
quantityCombo.addItem(i);
works out of the box - because the compiler does auto-boxing to create an Integer object from the provided int value.