---
title: ComboBox
order: 16
layout: page
---

[[components.combobox]]
= ComboBox

ifdef::web[]
[.sampler]
image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/multiple-value/combo-box"]
endif::web[]

[classname]#ComboBox# is a selection component allows selecting an item from a
drop-down list. The component also has a text field area, which allows entering
search text by which the items shown in the drop-down list are filtered. Common
selection component features are described in
<<dummy/../../../framework/components/components-selection#components.selection,"Selection
Components">>.

.The [classname]#ComboBox# Component
image::img/combobox-basic.png[width=35%, scaledwidth=50%]

[[components.combobox.filtering]]
== Filtered Selection

[classname]#ComboBox# allows filtering the items available for selection in the
drop-down list by the text entered in the input box. To apply custom filtering, the [methodname]#setItems(CaptionFilter, Collection)# can be used. When using a [classname]#DataProvider#, the filtering is delegated to it.

[[figure.components.combobox.filter]]
.Filtered Selection in [classname]#ComboBox#
image::img/combobox-filtering.png[width=35%, scaledwidth=50%]

Pressing kbd:[Enter] will complete the item in the input box. Pressing kbd:[Up] and kbd:[Down] arrow keys can be used for selecting an item from the drop-down list. The
drop-down list is paged and clicking on the scroll buttons will change to the
next or previous page. The list selection can also be done with the arrow keys
on the keyboard. The shown items are loaded from the server as needed, so the
number of items held in the component can be quite large. The number of matching
items is displayed by the drop-down list.

[[components.combobox.newitems]]
== Allowing Adding New Items

[classname]#ComboBox# allows the user to add new items, when the user types
in a value and presses kbd:[Enter]. You need to enable this with
[methodname]#setNewItemHandler()#.

Adding new items is not possible if the selection component is read-only. An
attempt to do so may result in an exception.


=== Handling New Items

Adding new items is handled by a [interfacename]#NewItemHandler#, which gets the
item caption string as parameter for the [methodname]#accept(String)# method.


[source, java]
----
// List of planets
List<Planet> planets = new ArrayList<>();
planets.add(new Planet(1, "Mercury"));
planets.add(new Planet(2, "Venus"));
planets.add(new Planet(3, "Earth"));

ComboBox<Planet> select =
    new ComboBox<>("Select or Add a Planet");
select.setItems(planets);

// Use the name property for item captions
select.setItemCaptionGenerator(Planet::getName);

// Allow adding new items and add
// handling for new items
select.setNewItemHandler(inputString -> {

    Planet newPlanet = new Planet(planets.size(), inputString);
    planets.add(newPlanet);

    // Update combobox content
    select.setItems(planets);

    // Remember to set the selection to the new item
    select.setSelectedItem(newPlanet);
});
----

[[components.combobox.css]]
== CSS Style Rules


[source, css]
----
.v-filterselect { }
  .v-filterselect-input { }
  .v-filterselect-button { }

// Under v-overlay-container
.v-filterselect-suggestpopup { }
  .popupContent { }
    .v-filterselect-prevpage,
      .v-filterselect-prevpage-off { }
    .v-filterselect-suggestmenu { }
      .gwt-MenuItem { }
    .v-filterselect-nextpage,
      .v-filterselect-nextpage-off { }
    .v-filterselect-status { }
----

In its default state, only the input field of the [classname]#ComboBox#
component is visible. The entire component is enclosed in
[literal]#++v-filterselect++# style (a legacy remnant), the input field has
[literal]#++v-filterselect-input++# style and the button in the right end that
opens and closes the drop-down result list has
[literal]#++v-filterselect-button++# style.

The drop-down result list has an overall
[literal]#++v-filterselect-suggestpopup++# style. It contains the list of
suggestions with [literal]#++v-filterselect-suggestmenu++# style. When there are
more items that fit in the menu, navigation buttons with
[literal]#++v-filterselect-prevpage++# and
[literal]#++v-filterselect-nextpage++# styles are shown. When they are not
shown, the elements have [literal]#++-off++# suffix. The status bar in the
bottom that shows the paging status has [literal]#++v-filterselect-status++#
style.
ption value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/jquery-ui.git/tree/?h=dependabot/github_actions/github-actions-384b59e5c3&amp;id=6843ced12e4051aefbee47cf87fa79794737eb8a'>root</a>/<a href='/jquery-ui.git/tree/demos?h=dependabot/github_actions/github-actions-384b59e5c3&amp;id=6843ced12e4051aefbee47cf87fa79794737eb8a'>demos</a>/<a href='/jquery-ui.git/tree/demos/checkboxradio?h=dependabot/github_actions/github-actions-384b59e5c3&amp;id=6843ced12e4051aefbee47cf87fa79794737eb8a'>checkboxradio</a>/<a href='/jquery-ui.git/tree/demos/checkboxradio/default.html?h=dependabot/github_actions/github-actions-384b59e5c3&amp;id=6843ced12e4051aefbee47cf87fa79794737eb8a'>default.html</a></div><div class='content'>blob: 064967c12193e022895cdfcd1e031884853916f2 (<a href='/jquery-ui.git/plain/demos/checkboxradio/default.html?h=dependabot/github_actions/github-actions-384b59e5c3&amp;id=6843ced12e4051aefbee47cf87fa79794737eb8a'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
<a id='n2' href='#n2'>2</a>
<a id='n3' href='#n3'>3</a>
<a id='n4' href='#n4'>4</a>
<a id='n5' href='#n5'>5</a>
<a id='n6' href='#n6'>6</a>
<a id='n7' href='#n7'>7</a>
<a id='n8' href='#n8'>8</a>
<a id='n9' href='#n9'>9</a>
<a id='n10' href='#n10'>10</a>
<a id='n11' href='#n11'>11</a>
<a id='n12' href='#n12'>12</a>
<a id='n13' href='#n13'>13</a>
<a id='n14' href='#n14'>14</a>
<a id='n15' href='#n15'>15</a>
<a id='n16' href='#n16'>16</a>
<a id='n17' href='#n17'>17</a>
<a id='n18' href='#n18'>18</a>
<a id='n19' href='#n19'>19</a>
<a id='n20' href='#n20'>20</a>
<a id='n21' href='#n21'>21</a>
<a id='n22' href='#n22'>22</a>
<a id='n23' href='#n23'>23</a>
<a id='n24' href='#n24'>24</a>
<a id='n25' href='#n25'>25</a>
<a id='n26' href='#n26'>26</a>
<a id='n27' href='#n27'>27</a>
<a id='n28' href='#n28'>28</a>
<a id='n29' href='#n29'>29</a>
<a id='n30' href='#n30'>30</a>
<a id='n31' href='#n31'>31</a>
<a id='n32' href='#n32'>32</a>
<a id='n33' href='#n33'>33</a>
<a id='n34' href='#n34'>34</a>
<a id='n35' href='#n35'>35</a>
<a id='n36' href='#n36'>36</a>
<a id='n37' href='#n37'>37</a>
<a id='n38' href='#n38'>38</a>
<a id='n39' href='#n39'>39</a>
<a id='n40' href='#n40'>40</a>
<a id='n41' href='#n41'>41</a>
<a id='n42' href='#n42'>42</a>
<a id='n43' href='#n43'>43</a>
<a id='n44' href='#n44'>44</a>
<a id='n45' href='#n45'>45</a>
<a id='n46' href='#n46'>46</a>
<a id='n47' href='#n47'>47</a>
<a id='n48' href='#n48'>48</a>
<a id='n49' href='#n49'>49</a>
<a id='n50' href='#n50'>50</a>
<a id='n51' href='#n51'>51</a>
<a id='n52' href='#n52'>52</a>
<a id='n53' href='#n53'>53</a>
<a id='n54' href='#n54'>54</a>
<a id='n55' href='#n55'>55</a>
<a id='n56' href='#n56'>56</a>
<a id='n57' href='#n57'>57</a>
<a id='n58' href='#n58'>58</a>
<a id='n59' href='#n59'>59</a>
<a id='n60' href='#n60'>60</a>
<a id='n61' href='#n61'>61</a>
<a id='n62' href='#n62'>62</a>
<a id='n63' href='#n63'>63</a>
</pre></td>
<td class='lines'><pre><code>