Combo box

A combo box is a drop-down list allowing you to choose one option from a list of options.

A WComboBox corresponds to an HTML <select> element.

Example
source
  void ComboBox() {
    WComboBox cb = new WComboBox();
    cb.addItem("Heavy");
    cb.addItem("Medium");
    cb.addItem("Light");
  }

WComboBox is a View widget (see also Model-View-Controller) which instantiates its own WStringListModel by default. You can use this widget also in conjunction with another model.

Events

You can capture the selected item and deal with it using a signal/slot mechanism.

Example
source
  void ComboBoxActivated() {
    WContainerWidget container = new WContainerWidget();
    final WComboBox cb = new WComboBox((WContainerWidget) container);
    cb.addItem("Heavy");
    cb.addItem("Medium");
    cb.addItem("Light");
    cb.setCurrentIndex(1);
    cb.setMargin(new WLength(10), EnumSet.of(Side.Right));
    final WText out = new WText((WContainerWidget) container);
    out.addStyleClass("help-block");
    cb.changed()
        .addListener(
            this,
            () -> {
              out.setText(new WString("You selected {1}.").arg(cb.getCurrentText()));
            });
  }

Top

Model

WComboBox is an MVC class (model-view-controller). By default a WStringListModel is used. With this model you can associate a single column of data to the displayed items. The member methods addItem(), insertItem() and removeItem() manipulate the model. You can set the model with setModel().

Item models support different roles like JWt.ItemDataRole.DisplayRole and JWt.ItemDataRole.UserRole. The text for an item in the drop-down box is associated with a JWt.ItemDataRole.DisplayRole. Typically, you will associate the underlying "value" with a JWt.ItemDataRole.UserRole). In this way, you can also add additional user roles.

Note that there are still other models like WFormModel which and can be used to represent fields in a form.

Example
source
  void ComboBoxModel() {
    WContainerWidget container = new WContainerWidget();
    final WComboBox cb = new WComboBox((WContainerWidget) container);
    cb.setMargin(new WLength(10), EnumSet.of(Side.Right));
    final WStringListModel model = new WStringListModel();
    model.addString("Belgium");
    model.setData(0, 0, "BE", ItemDataRole.User);
    model.addString("Netherlands");
    model.setData(1, 0, "NL", ItemDataRole.User);
    model.addString("United Kingdom");
    model.setData(2, 0, "UK", ItemDataRole.User);
    model.addString("United States");
    model.setData(3, 0, "US", ItemDataRole.User);
    model.setFlags(3, EnumSet.of(ItemFlag.Selectable));
    cb.setNoSelectionEnabled(true);
    cb.setModel(model);
    final WText out = new WText((WContainerWidget) container);
    out.addStyleClass("help-block");
    cb.changed()
        .addListener(
            this,
            () -> {
              WString countryName = cb.getCurrentText();
              int row = cb.getCurrentIndex();
              WString countryCode =
                  StringUtils.asString(model.getData(model.getIndex(row, 0), ItemDataRole.User));
              out.setText(
                  new WString("You selected {1} with key {2}.").arg(countryName).arg(countryCode));
            });
  }

Top