Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 956
Richeve Bebedor Avatar asked May 04 '11 21:05

Richeve Bebedor


People also ask

What is the difference between JList and JComboBox?

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.

Which method is used to add items to JComboBox?

Creates a JComboBox with a default data model. The default data model is an empty list of objects. Use addItem to add items.

What is JComboBox?

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.


2 Answers

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.

like image 125
Vincent Ramdhanie Avatar answered Oct 22 '22 02:10

Vincent Ramdhanie


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.

like image 43
kleopatra Avatar answered Oct 22 '22 02:10

kleopatra