cboStart.addItem(new Integer(1));
cboStart.addItem(new Integer(2)); // and so one to 12
cboStart.addItem(1);
<pre><code>cboStart.addItem(2);</code></pre>
// 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 ifdaniel50096230 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)) ...
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
This spell it out all nicely
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
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.
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448