Obtaining value from combo-box in netbeans GUI

49,819

Try

jComboBox.getSelectedItem()

And perhaps the following snipped might be useful to get the string.

Object selectedItem = comboBox.getSelectedItem();
if (selectedItem != null)
{
    String selectedItemStr = selectedItem.toString();
    Foo(selectedItemStr); // Some method that takes a string parameter.
}

to get the selected item.

Share:
49,819
T_Brown
Author by

T_Brown

Updated on May 22, 2020

Comments

  • T_Brown
    T_Brown almost 4 years

    Basically I've been experimenting with the use of drop-boxes in a GUI I'm making in netbeans. I know how easy it is to obtain a string variable from a text field using the .getText() method, however I would like to get a value that a user selects from a drop-down menu (i.e. the combo box), hits a 'submit' button (which the actionPerformed method is attached to) and place it in a string variable.

    This would allow me to use this string variable to pass into a set() method in another class.

    This is an annoying little problem but I expected there to be a few when I started.

    Any help appreciated.

  • T_Brown
    T_Brown about 13 years
    Yeah, that works fine. Had something along those lines but was trying to use a .getSource(). Silly of me I know. Thanks very much again. I will accept the answer as soon as it lets me.