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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

JComboBox Getting the selection(java)

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


    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 Posts: 2,031 ✭✭✭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,516 ✭✭✭BrokenArrows


    ya i used the following

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

    It worked perfectly,


Advertisement