Richeve Bebedor
Richeve Bebedor

Reputation: 1658

JComboBox setText Method

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!");

Upvotes: 5

Views: 36885

Answers (2)

kleopatra
kleopatra

Reputation: 51536

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.

Upvotes: 9

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103155

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.

Upvotes: 16

Related Questions