T_Brown
T_Brown

Reputation: 49

Obtaining value from combo-box in netbeans GUI

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.

Upvotes: 2

Views: 54096

Answers (1)

Bala R
Bala R

Reputation: 108937

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.

Upvotes: 9

Related Questions