Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JComboBox Getting the selection(java)

Options
  • 29-03-2005 3:58pm
    #1
    Registered Users Posts: 7,498 ✭✭✭


    T the moment i am creating a form that users have to fill out. On the form the user must make a selection from a jComboBox. This selection is then written to a sql database.

    My problem is that String selection = jcombobox.getText(); wont work because it is not a text box. Is there any other command that will achieve the same effect.

    I need to do it this way. Dont ask why!


Comments

  • Closed Accounts Posts: 7 arcdestroyer


    One of the easiest ways would be to put an actionlistener on the combobox and use the getSource() and getSelectedItem() methods as follows:

    public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String itemSelected = (String)cb.getSelectedItem();
    }


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    Of course you can call getSelectedItem() whenever you need it, you dont have to attach it to an ActionListener.

    Btw javadocs are your friend. They will tell you what methods are available in each class ;)


  • Registered Users Posts: 7,498 ✭✭✭BrokenArrows


    ya i used the following

    String selection = (String)comboBox.setSelectedItem();

    It worked perfectly,


Advertisement