how to set to nothing a ComboBox in java

ht‮/:spt‬/www.lautturi.com
how to set to nothing a ComboBox in java

To set a ComboBox to nothing in Java, you can use the setSelectedIndex method and pass it the value -1 as an argument. This will clear the selection and set the ComboBox to nothing.

Here's an example of how you can do this:

JComboBox comboBox = new JComboBox();
comboBox.setSelectedIndex(-1);

Alternatively, you can use the setSelectedItem method and pass it null as an argument to clear the selection and set the ComboBox to nothing.

Here's an example of how you can use the setSelectedItem method:

JComboBox comboBox = new JComboBox();
comboBox.setSelectedItem(null);

Note that these methods will only work if the ComboBox is editable. If the ComboBox is not editable, you will need to use a different approach to clear the selection.

Created Time:2017-11-01 20:43:01  Author:lautturi