| | |
JCombobox
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
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?
Java Syntax (Toggle Plain Text)
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
Last edited by puneetkay; Sep 18th, 2008 at 1:55 am.
•
•
•
•
cboStart.addItem(new Integer(1)); cboStart.addItem(new Integer(2)); // and so one to 12 cboStart.addItem(1);// and so one to 12Java Syntax (Toggle Plain Text)
cboStart.addItem(2);
Use any of these two methods.
Regards,
PuneetK
I Think the second is wrong since if I remember correctly the addItem() method takes Objects as arguments.
Better user this:
Java Syntax (Toggle Plain Text)
for (int i=1;i<=12;i++) { cboStart.addItem(new Integer(i)); //Integer objects in the ComboBox }
Java Syntax (Toggle Plain Text)
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 daniel50096230 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)) ...
Last edited by javaAddict; Sep 18th, 2008 at 4:26 am.
Check out my New Bike at my Public Profile at the "About Me" tab
This spell it out all nicely
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
•
•
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?
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
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
•
•
•
•
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.
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. ![]() |
Similar Threads
- Set / Get jComboBox value and Item. (Java)
- horizontal line/rule in JComboBox? (Java)
- comparing a value to a value in a jComboBox (Java)
- Display Value in JcomboBox (Java)
- JComboBox -- Item is not in the list???????? (Java)
- java uses or overrides a deprecated API?? (Java)
Other Threads in the Java Forum
- Previous Thread: Login Successful, close Login file and Open another file
- Next Thread: Modifying a JFrame
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






