Reddy
Reddy

Reputation: 1628

Retrieve text from JComboBox

How to retrieve Text that is typed in JComboBox. This text need not be an existing item.

Upvotes: 9

Views: 49380

Answers (3)

Marcelo
Marcelo

Reputation: 1

You can try the following

String typedText = ((JTextField)myComboBox.getEditor().getEditorComponent()).getText();

Upvotes: 0

Mehdi
Mehdi

Reputation: 1477

Simply use :

String value= comboBox.getSelectedItem().toString();

Other examples available here

Upvotes: 3

Carlos
Carlos

Reputation: 2513

You can get the selected or typed value from a JComboBox by calling method getSelectedItem. If it is not an existing item, then you'll get a String object. Otherwise you'll get whatever object you populated the combo box with.

Upvotes: 18

Related Questions