java get JComboBox value

java get JComboBox value

To get the value of a JComboBox in Java, you can use the getSelectedItem method of the JComboBox class.

Here is an example of how to get the value of a JComboBox:

JComboBox<String> comboBox = new JComboBox<>();
comboBox.addItem("Option 1");
comboBox.addItem("Option 2");
comboBox.addItem("Option 3");

String selectedValue = (String) comboBox.getSelectedItem();
Source:ww‮al.w‬utturi.com

The getSelectedItem method returns the currently selected item in the JComboBox, which you can then cast to the appropriate type (in this case, String).

If you want to get the index of the selected item instead of the value, you can use the getSelectedIndex method. For example:

int selectedIndex = comboBox.getSelectedIndex();

This will return the index of the selected item in the JComboBox, or -1 if no item is selected.

Created Time:2017-11-03 22:21:07  Author:lautturi