Package org.zkoss.zul

Class Combobox

All Implemented Interfaces:
Serializable, Cloneable, Component, Disable, Readonly, Scope, ComponentCtrl, Constrainted

public class Combobox extends Textbox
A combobox.

Non-XUL extension. It is used to replace XUL menulist. This class is more flexible than menulist, such as setAutocomplete(boolean) setAutodrop(boolean).

Default getZclass(): z-combobox.(since 3.5.0)

Events: onOpen, onSelect, onAfterRender
Developers can listen to the onOpen event and initializes it when OpenEvent.isOpen() is true, and/or clean up if false.
onAfterRender is sent when the model's data has been rendered.(since 5.0.4)

Besides assign a list model, you could assign a renderer (a ComboitemRenderer instance) to a combobox, such that the combobox will use this renderer to render the data returned by ListModel.getElementAt(int). If not assigned, the default renderer, which assumes a label per combo item, is used. In other words, the default renderer adds a label to a row by calling toString against the object returned by ListModel.getElementAt(int). (since 3.0.2)

Note: to have better performance, onOpen is sent only if a non-deferrable event listener is registered (see Deferrable).

Like Datebox, the value of a read-only comobobox (InputElement.isReadonly()) can be changed by dropping down the list and selecting an combo item (though users cannot type anything in the input box).

Author:
tomyeh
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • coerceToString

      protected String coerceToString(Object value)
      Description copied from class: Textbox
      Coerces the value passed to Textbox.setValue(java.lang.String).

      Default: convert null to an empty string.

      Overrides:
      coerceToString in class Textbox
    • getModel

      public <T> ListModel<T> getModel()
      Returns the list model associated with this combobox, or null if this combobox is not associated with any list data model.

      Note: for implementation of auto-complete, the result of getItemCount() is a subset of model. So, if the model implemented ListSubModel interface, you can't use the index of model to find the comboitem by getItemAtIndex(int).

      Since:
      3.0.2
      See Also:
    • setModel

      public void setModel(ListModel<?> model)
      Sets the list model associated with this combobox. If a non-null model is assigned, no matter whether it is the same as the previous, it will always cause re-render.
      Parameters:
      model - the list model to associate, or null to dissociate any previous model.
      Throws:
      UiException - if failed to initialize with the model
      Since:
      3.0.2
    • setEmptySearchMessage

      public void setEmptySearchMessage(String msg)
      Sets empty search message. This message would be displayed, when no matching results was found. Note: it's meaningless if no model case.
      Parameters:
      msg -
      Since:
      8.5.1
    • getEmptySearchMessage

      public String getEmptySearchMessage()
      Returns the empty search message if any. Default: null
      Since:
      10.0.0
    • getItemRenderer

      public <T> ComboitemRenderer<T> getItemRenderer()
      Returns the renderer to render each row, or null if the default renderer is used.
      Since:
      3.0.2
    • setItemRenderer

      public void setItemRenderer(ComboitemRenderer<?> renderer)
      Sets the renderer which is used to render each row if getModel() is not null.

      Note: changing a render will not cause the combobox to re-render. If you want it to re-render, you could assign the same model again (i.e., setModel(getModel())), or fire an ListDataEvent event.

      Parameters:
      renderer - the renderer, or null to use the default.
      Throws:
      UiException - if failed to initialize with the model
      Since:
      3.0.2
    • setItemRenderer

      Sets the renderer by use of a class name. It creates an instance automatically.
      Throws:
      ClassNotFoundException
      NoSuchMethodException
      IllegalAccessException
      InstantiationException
      InvocationTargetException
      Since:
      3.0.2
    • onInitRender

      public void onInitRender(Event data)
      Handles a private event, onInitRender. It is used only for implementation, and you rarely need to invoke it explicitly.
      Since:
      3.0.2
    • isAutodrop

      public boolean isAutodrop()
      Returns whether to automatically drop the list if users is changing this text box.

      Default: false.

    • setAutodrop

      public void setAutodrop(boolean autodrop)
      Sets whether to automatically drop the list if users is changing this text box.
    • isAutocomplete

      public boolean isAutocomplete()
      Returns whether to automatically complete this text box by matching the nearest item (Comboitem. It is also known as auto-type-ahead.

      Default: true (since 5.0.0).

      If true, the nearest item will be searched and the text box is updated automatically. If false, user has to click the item or use the DOWN or UP keys to select it back.

      Don't confuse it with the auto-completion feature mentioned by other framework. Such kind of auto-completion is supported well by listening to the onChanging event.

    • setAutocomplete

      public void setAutocomplete(boolean autocomplete)
      Sets whether to automatically complete this text box by matching the nearest item (Comboitem.
    • isOpen

      public boolean isOpen()
      Returns whether this combobox is open.

      Default: false.

      Since:
      6.0.0
    • setOpen

      public void setOpen(boolean open)
      Drops down or closes the list of combo items (Comboitem. only works while visible
      Since:
      3.0.1
    • open

      public void open()
      Drops down the list of combo items (Comboitem. It is the same as setOpen(true).
      Since:
      3.0.1
    • close

      public void close()
      Closes the list of combo items (Comboitem if it was dropped down. It is the same as setOpen(false).
      Since:
      3.0.1
    • isButtonVisible

      public boolean isButtonVisible()
      Returns whether the button (on the right of the textbox) is visible.

      Default: true.

    • setButtonVisible

      public void setButtonVisible(boolean visible)
      Sets whether the button (on the right of the textbox) is visible.
    • isInstantSelect

      public boolean isInstantSelect()
      Returns true if onSelect event is sent as soon as user selects using keyboard navigation.

      Default: true

      Since:
      8.6.1
    • setInstantSelect

      public void setInstantSelect(boolean instantSelect)
      Sets the instantSelect attribute. When the attribute is true, onSelect event will be fired as soon as user selects using keyboard navigation. If the attribute is false, user needs to press Enter key to finish the selection using keyboard navigation.
      Since:
      8.6.1
    • getItems

      public List<Comboitem> getItems()
      Returns a 'live' list of all Comboitem. By live we mean you can add or remove them directly with the List interface.

      Currently, it is the same as AbstractComponent.getChildren(). However, we might add other kind of children in the future.

    • getItemCount

      public int getItemCount()
      Returns the number of items.
    • getItemAtIndex

      public Comboitem getItemAtIndex(int index)
      Returns the item at the specified index.
    • appendItem

      public Comboitem appendItem(String label)
      Appends an item.
    • removeItemAt

      public Comboitem removeItemAt(int index)
      Removes the child item in the list box at the given index.
      Returns:
      the removed item.
    • getSelectedItem

      public Comboitem getSelectedItem()
      Returns the selected item.
      Since:
      2.4.0
    • setSelectedItem

      public void setSelectedItem(Comboitem item)
      Deselects the currently selected items and selects the given item.

      Note: if the label of comboitem has the same more than one, the first comboitem will be selected at client side, it is a limitation of Combobox and it is different from Listbox.

      Since:
      3.0.2
    • setSelectedIndex

      public void setSelectedIndex(int jsel)
      Deselects the currently selected items and selects the item with the given index.

      Note: if the label of comboitem has the same more than one, the first comboitem will be selected at client side, it is a limitation of Combobox and it is different from Listbox.

      Since:
      3.0.2
    • getSelectedIndex

      public int getSelectedIndex()
      Returns the index of the selected item, or -1 if not selected.
      Since:
      3.0.1
    • getPopupWidth

      public String getPopupWidth()
      Returns:
      the width of the popup of this component
      Since:
      8.0.3
    • setPopupWidth

      public void setPopupWidth(String popupWidth)
      Sets the width of the popup of this component. If the input is a percentage, the popup width will be calculated by multiplying the width of this component with the percentage. (e.g. if the input string is 130%, and the width of this component is 300px, the popup width will be 390px = 300px * 130%) Others will be set directly.
      Parameters:
      popupWidth - the width of the popup of this component
      Since:
      8.0.3
    • setMultiline

      public void setMultiline(boolean multiline)
      Description copied from class: Textbox
      Sets whether it is multiline.
      Overrides:
      setMultiline in class Textbox
    • setRows

      public void setRows(int rows)
      Description copied from class: Textbox
      Sets the rows.

      Note: Not allowed to set rows and height/vflex at the same time

      Overrides:
      setRows in class Textbox
    • getExtraCtrl

      public Object getExtraCtrl()
      Description copied from class: HtmlBasedComponent
      Returns the client control for this component. It is used only by component developers.

      Default: creates an instance of HtmlBasedComponent.ExtraCtrl.

      Specified by:
      getExtraCtrl in interface ComponentCtrl
      Overrides:
      getExtraCtrl in class HtmlBasedComponent
      Returns:
      null if no special handling required. If the component requires some special controls, it could return an object that implements one or several interfaces in the org.zkoss.zk.ui.ext.render package. For example, Cropper.
      See Also:
    • setIconSclass

      public void setIconSclass(String iconSclass)
      Sets the iconSclass name of this Combobox.
      Parameters:
      iconSclass - String
      Since:
      8.6.2
    • getIconSclass

      public String getIconSclass()
      Returns the iconSclass name of this Combobox.
      Returns:
      the iconSclass name
      Since:
      8.6.2
    • getZclass

      public String getZclass()
      Description copied from class: HtmlBasedComponent
      Returns the ZK Cascading Style class for this component. It usually depends on the implementation of the mold (AbstractComponent.getMold()).

      Default: null (the default value depends on element).

      HtmlBasedComponent.setZclass(java.lang.String)) will completely replace the default style of a component. In other words, the default style of a component is associated with the default value of HtmlBasedComponent.getZclass(). Once it is changed, the default style won't be applied at all. If you want to perform small adjustments, use HtmlBasedComponent.setSclass(java.lang.String) instead.

      Overrides:
      getZclass in class Textbox
      See Also:
    • renderProperties

      protected void renderProperties(ContentRenderer renderer) throws IOException
      Description copied from class: HtmlBasedComponent
      Renders the content of this component, excluding the enclosing tags and children.

      See also ZK Client-side Reference: Property Rendering

      Overrides:
      renderProperties in class Textbox
      Throws:
      IOException
    • service

      public void service(AuRequest request, boolean everError)
      Processes an AU request.

      Default: in addition to what are handled by InputElement.service(org.zkoss.zk.au.AuRequest, boolean), it also handles onOpen and onSelect.

      Specified by:
      service in interface ComponentCtrl
      Overrides:
      service in class InputElement
      everError - whether any error ever occurred before processing this request.
      Since:
      5.0.0
      See Also:
    • beforeChildAdded

      public void beforeChildAdded(Component newChild, Component refChild)
      Description copied from class: AbstractComponent
      Default: does nothing.
      Specified by:
      beforeChildAdded in interface ComponentCtrl
      Overrides:
      beforeChildAdded in class AbstractComponent
      Parameters:
      newChild - the child to be added (never null).
      refChild - another child component that the new child will be inserted before it. If null, the new child will be the last child.
      See Also:
    • isChildable

      protected boolean isChildable()
      Childable.
      Overrides:
      isChildable in class InputElement
    • onChildAdded

      public void onChildAdded(Component child)
      Description copied from class: AbstractComponent
      Default: handles special event listeners.
      Specified by:
      onChildAdded in interface ComponentCtrl
      Overrides:
      onChildAdded in class AbstractComponent
      See Also:
    • onChildRemoved

      public void onChildRemoved(Component child)
      Description copied from class: AbstractComponent
      Default: handles special event listeners.
      Specified by:
      onChildRemoved in interface ComponentCtrl
      Overrides:
      onChildRemoved in class AbstractComponent
      See Also:
    • clone

      public Object clone()
      Description copied from interface: Component
      Clones the component. All of its children and descendants are cloned. Also, ID are preserved.
      Specified by:
      clone in interface Component
      Overrides:
      clone in class Textbox
      Returns:
      the new component. Notice that it doesn't belong to any page, nor desktop. It doesn't have a parent, either.
    • sessionWillPassivate

      public void sessionWillPassivate(Page page)
      Description copied from interface: ComponentCtrl
      Notification that the session, which owns this component, is about to be passivated (a.k.a., serialized).

      Note: only root components are notified by this method.

      Specified by:
      sessionWillPassivate in interface ComponentCtrl
      Overrides:
      sessionWillPassivate in class AbstractComponent
    • sessionDidActivate

      public void sessionDidActivate(Page page)
      Description copied from interface: ComponentCtrl
      Notification that the session, which owns this component, has just been activated (a.k.a., deserialized).

      Note: only root components are notified by this method.

      Specified by:
      sessionDidActivate in interface ComponentCtrl
      Overrides:
      sessionDidActivate in class AbstractComponent
    • getPropertyAccess

      public PropertyAccess getPropertyAccess(String prop)
      Description copied from interface: ComponentCtrl
      Returns the corresponding property access object from the given property name, if any.
      Specified by:
      getPropertyAccess in interface ComponentCtrl
      Overrides:
      getPropertyAccess in class Textbox
      Parameters:
      prop - the name of the property
      Returns:
      null it means not to support for the property name.
    • onPageAttached

      public void onPageAttached(Page newpage, Page oldpage)
      Description copied from class: AbstractComponent
      Default: handles special event listeners.
      Specified by:
      onPageAttached in interface ComponentCtrl
      Overrides:
      onPageAttached in class AbstractComponent
      Parameters:
      newpage - the new page (never null).
      oldpage - the previous page, if any, or null if it didn't belong to any page.
      See Also:
    • onPageDetached

      public void onPageDetached(Page page)
      Description copied from class: AbstractComponent
      Default: handles special event listeners.
      Specified by:
      onPageDetached in interface ComponentCtrl
      Overrides:
      onPageDetached in class AbstractComponent
      Parameters:
      page - the previous page (never null)
      See Also: