Menu

HowtoCreateRadioGroups

Remo

Creating and binding Radio Groups

There are 2 major stories how radio groups are used:

  1. as alternative to a combo box to select enum values (Java enums and other enum patterns, even dynamically read from a database)
  2. as alternative to a checkbox to select a boolean value

In both stories there are two possibilities to create the UI:

  1. create the widgets using standard widget of type RCPRadioButton having full control of layout etc
  2. let the framework create the radio buttons automatically

All scenarios have in common that radio buttons are created using RCPRadioButton and you wrap these buttons together with their associated model values into a RadioGroupManager?; this manager represents the value of the group.

This RadioGroupManager? can be bound to a model property using validationManager.bindValue(RadioGroupManager?,...). The binding bidirectionally synchronizes the model value to the RadioGroupManager? value.

There are some convenience methods provided which do some of these actions behind the scenes, but the technical implementation always is the same.

Dynamically creating radios for an enum

This represents 1b from the alternatives above. Make sure the enum has a range adapter registered in the ModelAdapter?; the default bean model adapter has one registered for standard java enums which is used here.

  1. create a property in the data model of type e.g. GeschlechtCode?

    public static final String P_Geschlecht = "geschlecht";
    private GeschlechtCode m_Geschlecht;
    public GeschlechtCode getGeschlecht()
    {
      return m_Geschlecht;
    }
    
    public void setGeschlecht(GeschlechtCode value)
    {
      Object oldValue = m_Geschlecht;
      m_Geschlecht = value;
      propertyChangeSupport.firePropertyChange(P_Geschlecht, oldValue, value);
    }
    
  2. in the createUI() method of the view model let the builder dynamically create the radios for all code values:

    private RCPRadioGroup geschlechtGroup = new RCPRadioGroup("Geschlecht:");
    
    /** each radio group needs a manager */
    private RadioGroupManager geschlechtRadioGroupManager;
    
    public void createUI(FormToolkit toolkit, Composite parent)
    {
      ...
      // create a group for the radio buttons, if you dont like group borders, use a composite instead.
      // You can even use the section directly, anyhow the createEnumRadios group creates each radio
      // eating up one cell, so make sure to get your layout right.
      builder.addContainer(m_geschlechtGroup,1);
      // radio buttons are not created here yet
    }
    
  3. In the bind method use bindValue to bind the created RadioGroupManager? to the model property:

    public void bind(ValidationManager v, Object model)
    {
      ...
      // validation manager creates radio buttons dynamically in method handleRangeAdapter
      // and sets the value to the radio group manager
      v.bindValue(geschlechtRadioGroupManager, model, Sandbox2DataModel.P_Geschlecht);
    }
    

Creating radio buttons yourself and binding to a Boolean

This represents 2a from the alternatives above.

  1. create a boolean property in the model of type Boolean or boolean; recommended ist Boolean to be able to handle null values

    public static final String P_OverdrawAccount = "overdrawAccount";
    private Boolean m_OverdrawAccount;

    public Boolean getOverdrawAccount()
    {
    return (Boolean) m_OverdrawAccount;
    }

    public void setOverdrawAccount(Boolean value)
    {
    Object oldValue = m_OverdrawAccount;
    m_OverdrawAccount = value;
    propertyChangeSupport.firePropertyChange(P_OverdrawAccount, oldValue, value);
    }

  2. in the createUI() method of the view model create the radios yourself using standard mechanism:

    public void createUI(FormToolkit toolkit, Composite parent)
    {
       m_kontoueberzugYesRadio = new RCPSimpleButton("Konto überziehbar", SWT.RADIO);
       m_kontoueberzugNoRadio = new RCPSimpleButton("Konto nicht überziehbar", SWT.RADIO);
    
                  ...
       widgetBuilder.fill(1).add(m_kontoueberzugYesRadio);
       widgetBuilder.fillLine();
       widgetBuilder.addSkipLeftLine(m_kontoueberzugNoRadio, 1);
    }
    
  3. In the bind method use bindRadioToBoolean to implicitly create a RadioGroupManager? and bind it to the model propertiy:

    public void bind(ValidationManager context, Object dataModel)
    {
      ...
      // bind explicitly using a radio group manager
      context.bindRadioToBoolean(m_kontoueberzugYesRadio, m_kontoueberzugNoRadio, dataModel, TestModel.P_OverdrawAccount);
    }
    

Creating radio buttons yourself and binding to whatever objects you like

This is the most flexible usage of radio group binding. Actually it follows the same pattern as binding to Boolean, but you create and bind your RadioGroupManager? yourself. In the example above you enhance the createUI() method to create a RadioGroupManager? explicitly which associates radio buttons with model values:

  1. model as above
  2. in the createUI() method of the view model create the radios yourself using standard DlamWidget? mechanism:

    public void createUI(FormToolkit toolkit, Composite parent)
    {
       m_kontoueberzugYesRadio = new RCPSimpleButton("Konto überziehbar", SWT.RADIO);
       m_kontoueberzugNoRadio = new RCPSimpleButton("Konto nicht überziehbar", SWT.RADIO);
    
                  ...
       widgetBuilder.addLine(m_kontoueberzugYesRadio);
       widgetBuilder.addLine(m_kontoueberzugNoRadio);
    }
    
  3. In the bind method explicitly create RadioGroupManager? and bind it:

    public void bind(ValidationManager context, Object dataModel)
    {
      ...
      // bind explicitly using a radio group manager
      RadioGroupManager rgm = new RadioGroupManager(new RCPSimpleButton[]{
      m_kontoueberzugYesRadio, m_kontoueberzugNoRadio}, new Object[]{Boolean.TRUE,
      Boolean.FALSE});
      context.bindValue(rgm, dataModel, TestModel.P_OverdrawAccount);
    }
    

Where to create Radio buttons

  • If you want state support (e.g. en/disable, required flag), you need to create radio buttons in a Group widget since this is where the state will be shown. This widget will display a border and a heading for the radio group.
  • If you dont need state and dont want a border or heading it is recommended to use a nested Composite to put the radios into.
  • If you want to put the radios into your section, you need to create your widgets yourself or make sure the section layout fits the standard one cell span of automatically created buttons.

State Binding for Radio Buttons

The state binding for radio buttons works not exactly like the state binding for other widgets, but similar. You have to use a Radio Group and bind to the radio group widget, (see [HowtoStateBinding?]) not to the radio buttons themselves since state is maintained for the group.

The RadioGroupManager?.getSelection() method returns the model object associated with the currently selected button, if you set a model object via setSelection() the associated button will be selected. A null value will be associated with no selected button.

If the group is required, a radio button must be selected.


Related

Documentation: ManualHowtos

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.