Email this to a friend
 
posted on May 27th 2015, at 15:25
by lunarg

Preferred:

String x = JComboBox.getSelectedItem().toString();

or

String x = String.valueOf(JComboBox.getSelectedItem());

The second method protects against null values as well.

Avoid using casting:

String x = (String)JComboBox.getSelectedItem();

This would work fine if the item is indeed a string, but will fail if it can (also) be any other data type. To be safe, use either of the first two methods.

Send a link to this post to yourself or a friend.

Send to e-mail:
Your name:
Your e-mail:
Captcha:
Type the letters and numbers as shown.
/get/captcha/1711698380
Not readable? Get another.
 
Information entered is solely used for sending a one-time e-mail, and is not retained and/or passed on to a third party.
 
 
 
 
« March 2024»
SunMonTueWedThuFriSat
     12
3456789
10111213141516
17181920212223
24252627282930
31      
 
Links
 
Quote
« Debating Windows vs. Linux vs. Mac is pointless: they all have their merits and flaws, and it ultimately comes to down to personal preference. »
Me