Search code examples
javaswinguser-interfacejcomboboxjtextfield

Field that acts both as textfield and combobox


Basically I need a field (for Swing) which lets user enter data if that specified data is not in the list provided in Combo-box. How to achieve this?


Solution

  • Check out the "Editable Combo Box" on the swing tutorial.

    Basically, you need to use setEditable(true) for your combo box. From the API docs:

    Determines whether the JComboBox field is editable. An editable JComboBox allows the user to type into the field or selected an item from the list to initialize the field, after which it can be edited. (The editing affects only the field, the list item remains intact.) A non editable JComboBox displays the selected item in the field, but the selection cannot be modified.