Comments
 
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.

Add a new comment
 
Your name:
Your e-mail:
Your comment:
 
Basic BBcode is supported.
Captcha:
Type the letters and numbers as shown.
/get/captcha/1722075270
Not readable? Get another.
 
 
 
 
« July 2024»
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031   
 
Links
 
Quote
« I needed a password with eight characters so I picked Snow White and the Seven Dwarves. »