943,542 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1922
  • Java RSS
Sep 18th, 2008
0

JCombobox

Expand 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daniel50096230 is offline Offline
4 posts
since Sep 2008
Sep 18th, 2008
0

Re: JCombobox

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)
  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.
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Sep 18th, 2008
0

Re: JCombobox

Click to Expand / Collapse  Quote originally posted by puneetkay ...
		cboStart.addItem(new Integer(1));
		cboStart.addItem(new Integer(2));  // and so one to 12
		
		cboStart.addItem(1);
		  
Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  1. for (int i=1;i<=12;i++) {
  2. cboStart.addItem(new Integer(i)); //Integer objects in the ComboBox
  3. }
or
Java Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Sep 18th, 2008
0

Re: JCombobox

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daniel50096230 is offline Offline
4 posts
since Sep 2008
Sep 18th, 2008
-1

Re: JCombobox

This spell it out all nicely
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Sep 18th, 2008
0

Re: JCombobox

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
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Sep 18th, 2008
0

Re: JCombobox

Click to Expand / Collapse  Quote originally posted by javaAddict ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Sep 18th, 2008
0

Re: JCombobox

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Login Successful, close Login file and Open another file
Next Thread in Java Forum Timeline: Modifying a JFrame





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC