javax.swing.JComboBox class does not support the setText method. Is there a way on how can I set the text of an editable combo box? Like something that I can call jcombobox1.setText("Text has changed!");
A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.
Creates a JComboBox with a default data model. The default data model is an empty list of objects. Use addItem to add items.
The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits JComponent class.
Once you set the JComboBox to editable you can do this:
String item = box.getEditor().getItem().toString();
To get the String the user typed or selected. And
box.getEditor().setItem("Text Has Changed");
to set your own text.
You can set the selectedItem to whatever value you want:
comboBox.setSelectedItem("text has changed");
Note that the selectedItem is explicitly documented to allow elements which are not part of the model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With