Help with jComboBox
Hello, I have a combo box, which a user selects a type of database(access, oracle or mysql). I need to determine which database the user is choosing, and am not quite sure how to get the value out of the combo box.
Have tried to use jComboBox.getSelectedItem(), but this returns an object, not a String.
A String Is a Object
What happens when you cast the result to a String?
String selection = (String)jComboBox.getSelectedItem();
Just so you know, Swing widgets return Objects as opposed to Strings so that you can use Objects other than Strings as your selection choices. It's relatively simple to do this: Just correctly override toString() in the Object type you are adding to the widget, and it'll display the text from toString(), but will return the actual object.
So as a design consideration, you might consider creating a database connection info object. One that encapsulates ALL the information required to connect. If this object overrides toString() with something meaningful - example "Accounting database" - then you will receive back a meaningful object that you can use, but you'll see the contents of the "toString()" method on the screen. As opposed to having to construct it after the selection is made.