Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

JComboBox Getting the selection(java)

  • 29-03-2005 03:58PM
    #1
    Registered Users, Registered Users 2 Posts: 7,544 ✭✭✭


    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, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭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, Registered Users 2 Posts: 7,544 ✭✭✭BrokenArrows


    ya i used the following

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

    It worked perfectly,


Advertisement