-1

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
    }
0

3 Answers 3

2
 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);
            }
Sign up to request clarification or add additional context in comments.

Comments

0

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));
}

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.