Radio buttons

JWt provides different kinds of button widgets. The WRadioButton class provides options which are usually mutually exclusive in contrast to WCheckBoxs which provide independent on/off options.

An instance of WRadioButton corresponds to an HTML <input type="radio"> element.

Use a WButtonGroup to group together radio buttons that reflect options that are mutually exclusive. With event handling you can follow up any change in the selection.

By default, radio buttons are inline. You will need to use setInline(false) to let them stack vertically.

Loose buttons

Example
source
  void RadioButtonsLoose() {
    WContainerWidget container = new WContainerWidget();
    new WRadioButton("Radio me!", (WContainerWidget) container);
    new WRadioButton("Radio me too!", (WContainerWidget) container);
  }

Button group

Usually, you'll group a set of radio buttons together in a WButtonGroup, so that only one can be selected at a time.

Example
source
  void RadioButtonGroup() {
    WContainerWidget container = new WContainerWidget();
    WButtonGroup group = new WButtonGroup();
    WRadioButton button;
    button = new WRadioButton("Radio me!", (WContainerWidget) container);
    group.addButton(button);
    button = new WRadioButton("No, radio me!", (WContainerWidget) container);
    group.addButton(button);
    button = new WRadioButton("Nono, radio me!", (WContainerWidget) container);
    group.addButton(button);
    group.setSelectedButtonIndex(0);
  }

Top

Stacked buttons

Since by default, radio buttons are inline, you will need to use setInline(false) to let them stack vertically.

Example
source
  void RadioButtonStack() {
    WContainerWidget container = new WContainerWidget();
    WButtonGroup group = new WButtonGroup();
    WRadioButton button;
    button = new WRadioButton("Radio me!", (WContainerWidget) container);
    button.setInline(false);
    group.addButton(button);
    button = new WRadioButton("No, radio me!", (WContainerWidget) container);
    button.setInline(false);
    group.addButton(button);
    button = new WRadioButton("Nono, radio me!", (WContainerWidget) container);
    button.setInline(false);
    group.addButton(button);
    group.setSelectedButtonIndex(0);
  }

Top

Events

You can process a new selection with a signal/slot mechanism. In the example below the signal checkedChanged() of the WButtonGroup is passed to an inner function passing a WPushButton. You can see that there are two ways to get the id assigned to a button, namely group->id(selection) and group->checkedId().

Example
source
  void RadioButtonsActivated() {
    WContainerWidget container = new WContainerWidget();
    WButtonGroup group = new WButtonGroup();
    WRadioButton rb;
    rb = new WRadioButton("sleeping", (WContainerWidget) container);
    rb.setInline(false);
    group.addButton(rb, 1);
    rb = new WRadioButton("eating", (WContainerWidget) container);
    rb.setInline(false);
    group.addButton(rb, 2);
    rb = new WRadioButton("driving", (WContainerWidget) container);
    rb.setInline(false);
    group.addButton(rb, 3);
    rb = new WRadioButton("learning Wt", (WContainerWidget) container);
    rb.setInline(false);
    group.addButton(rb, 4);
    group.setSelectedButtonIndex(0);
    final WText out = new WText((WContainerWidget) container);
    final WButtonGroup rawGroup = group;
    group
        .checkedChanged()
        .addListener(
            this,
            (WRadioButton selection) -> {
              WString text = new WString();
              switch (rawGroup.getId(selection)) {
                case 1:
                  text = new WString("You checked button {1}.").arg(rawGroup.getCheckedId());
                  break;
                case 2:
                  text = new WString("You selected button {1}.").arg(rawGroup.getCheckedId());
                  break;
                case 3:
                  text = new WString("You clicked button {1}.").arg(rawGroup.getCheckedId());
                  break;
              }
              text.append(new WString("... Are your really {1} now?").arg(selection.getText()));
              if (rawGroup.getId(selection) == 4) {
                text = new WString("That's what I expected!");
              }
              out.setText(new WString("<p>").append(text).append("</p>"));
            });
  }

Top