JCombobox

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 4
Reputation: daniel50096230 is an unknown quantity at this point 
Solved Threads: 0
daniel50096230 daniel50096230 is offline Offline
Newbie Poster

JCombobox

 
0
  #1
Sep 18th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: JCombobox

 
0
  #2
Sep 18th, 2008
Originally Posted by daniel50096230 View Post
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?
  1. cboStart.addItem(new Integer(1));
  2. cboStart.addItem(new Integer(2)); // and so one to 12
  3.  
  4. cboStart.addItem(1);
  5. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: JCombobox

 
0
  #3
Sep 18th, 2008
Originally Posted by puneetkay View Post
		cboStart.addItem(new Integer(1));
		cboStart.addItem(new Integer(2));  // and so one to 12
		
		cboStart.addItem(1);
		  
  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:

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

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: daniel50096230 is an unknown quantity at this point 
Solved Threads: 0
daniel50096230 daniel50096230 is offline Offline
Newbie Poster

Re: JCombobox

 
0
  #4
Sep 18th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,276
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: JCombobox

 
-1
  #5
Sep 18th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: JCombobox

 
0
  #6
Sep 18th, 2008
Originally Posted by daniel50096230 View Post
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
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: JCombobox

 
0
  #7
Sep 18th, 2008
Originally Posted by javaAddict View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: JCombobox

 
0
  #8
Sep 18th, 2008
Originally Posted by Ezzaral View Post
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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1094 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC