Results 1 to 3 of 3
  1. #1
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default 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:

  2. #2
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default

    A String Is a Object

    What happens when you cast the result to a String?
    Java Code:
    ...
     String selection = (String)jComboBox.getSelectedItem();
     ...
    Greetings.

    Albert:rolleyes:

  3. #3
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default

    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

  1. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  2. Canvas rendered over JCombobox menu
    By JSK in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2007, 08:00 PM
  3. Adding items to a jComboBox
    By tronovan in forum New To Java
    Replies: 0
    Last Post: 08-08-2007, 08:48 AM
  4. JComboBox setDisabledTextColor
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 05:32 AM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •