Results 1 to 3 of 3
Thread: Help with jComboBox
- 07-01-2007, 11:01 PM #1
Member
- Join Date
- Jun 2007
- Posts
- 92
- Rep Power
- 0
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.
Thanks.
Marcus:cool:
- 07-01-2007, 11:03 PM #2
Senior Member
- Join Date
- Jun 2007
- Posts
- 114
- Rep Power
- 0
A String Is a Object
What happens when you cast the result to a String?
Greetings.Java Code:... String selection = (String)jComboBox.getSelectedItem(); ...
Albert:rolleyes:
- 07-01-2007, 11:08 PM #3
Senior Member
- Join Date
- Jun 2007
- Posts
- 111
- Rep Power
- 0
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.
Greetings.
Eric
Similar Threads
-
How to change font/ font color etc in a graphic object using JCombobox?
By JavaInLove in forum AWT / SwingReplies: 5Last Post: 04-25-2009, 08:00 PM -
Canvas rendered over JCombobox menu
By JSK in forum AWT / SwingReplies: 2Last Post: 11-19-2007, 08:00 PM -
Adding items to a jComboBox
By tronovan in forum New To JavaReplies: 0Last Post: 08-08-2007, 08:48 AM -
JComboBox setDisabledTextColor
By Jack in forum AWT / SwingReplies: 2Last Post: 07-02-2007, 05:32 AM -
jcombobox
By Freddie in forum AWT / SwingReplies: 4Last Post: 05-11-2007, 12:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks