HI,
Which is the correct way to get the value from a JComboBox as a String and why is it the correct way. Thanks.
String x = JComboBox.getSelectedItem().
If you have only put (non-null) String
references in the JComboBox, then either way is fine.
However, the first solution would also allow for future modifications in which you insert Integer
s, Doubles
s, LinkedList
s etc. as items in the combo box.
To be robust against null
values (still without casting) you may consider a third option:
String x = String.valueOf(JComboBox.getSelectedItem());