Hi,I had created a Combobox called cboStart at the GUI form...Now I would like to add the integer of 1 to 12 into the combobox,may I know how can I do it?

Hi,I had created a Combobox called cboStart at the GUI form...Now I would like to add the integer of 1 to 12 into the combobox,may I know how can I do it?

cboStart.addItem(new Integer(1));
		cboStart.addItem(new Integer(2));  // and so one to 12
		
		cboStart.addItem(1);
		cboStart.addItem(2); // and so one to 12

Use any of these two methods.

Regards,
PuneetK

cboStart.addItem(new Integer(1));
boStart.addItem(new Integer(2)); // and so one to 12
cboStart.addItem(1);
cboStart.addItem(2); // and so one to 12

Use any of these two methods.

Regards,
PuneetK

The: cboStart.addItem(new Integer(1)) will add Integer objects to the ComboBox and you will have to cast them to Integer when you get the values.
I Think the second is wrong since if I remember correctly the addItem() method takes Objects as arguments.

Better user this:

for (int i=1;i<=12;i++) {
    cboStart.addItem(new Integer(i)); //Integer objects in the ComboBox
}

or

for (int i=1;i<=12;i++) {
    cboStart.addItem( String.valueOf(i) ); //String objects in the ComboBox. Need to be casted to String when getting the values
}

It is not smart to write 12 lines of code when you can simply use a for-loop. What if [I]daniel50096230[/I] wanted to add to a ComboBox numbers for 1 to 31? It would be stupid to write this 31 times:

cboStart.addItem(new Integer(1))
cboStart.addItem(new Integer(2)) ...

Great...The solution work fines...Thanks a lot...But I have a question here...I would like to add the integer start from 00 to 12,if I using Integer,it will only display 0 instead of 00...Is there any other way rather than using Integer?

This spell it out all nicely

Great...The solution work fines...Thanks a lot...But I have a question here...I would like to add the integer start from 00 to 12,if I using Integer,it will only display 0 instead of 00...Is there any other way rather than using Integer?

I think, the you better add items as String then you will be able to add 00, and use items index number.

By doing this, you will be able to add 00 and index will be same as value because you will start from 0. :)

Method : getSelectedIndex() and Int type return

Regards,
PuneetK

The: cboStart.addItem(new Integer(1)) will add Integer objects to the ComboBox and you will have to cast them to Integer when you get the values.
I Think the second is wrong since if I remember correctly the addItem() method takes Objects as arguments.

With Java 1.5 or later, Integer.valueOf(1) would be preferable to constructing a new Integer.

The second method, cboStart.addItem(1) also works just fine because auto-boxing will make the conversion to an Integer object for you.

The second method, cboStart.addItem(1) also works just fine because auto-boxing will make the conversion to an Integer object for you.

OK, good to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.