0

I am new to UI development. I need to create a page with 4 radio buttons in it which will render the form according to radio button i have selected. I am able to do all that but currently when i am loading the page no radio button is getting selected by default however rendering is done as i have made true fr it form constructor.

Can anyone help me how to make radio button checked when managing it from the managed

<ice:selectOneRadio style="color:#4D148C" id="legalHoldLevelId" partialSubmit="true"value="#legalHoldController.radioYesNo}" valueChangeListener="#{legalHoldController.onChangeOfRadioBttn}">
<f:selectItems value="#{legalHoldController.radioOptions}" />
</ice:selectOneRadio> 
public LegalHoldController() {
    setLegalCreationBox(true);
    setLegalRemoveBox(false);
    setLegalRevertRemoval(false);
    setAddToExisting(false);
}

public SelectItem[] getRadioOptions() {
    radioOptions[0] = new SelectItem(EDMPortalConstants.CREATE_LEGAL_HOLDS);
    radioOptions[1] = new SelectItem(EDMPortalConstants.ADD_TO_EXISTING);
    radioOptions[2] = new SelectItem(EDMPortalConstants.REMOVE_LEGAL_HOLDS);
    radioOptions[3] = new SelectItem(EDMPortalConstants.REVERT_REMOVAL);
    setRadioOptions(radioOptions);
    return radioOptions;
}

public void onChangeOfRadioBttn(ValueChangeEvent event) {
    String oldValue = (String) event.getNewValue();
    if (oldValue.equalsIgnoreCase(EDMPortalConstants.CREATE_LEGAL_HOLDS)) {
        setLegalCreationBox(true);
        setLegalRemoveBox(false);
        setLegalRevertRemoval(false);
        setAddToExisting(false);
        setRadioYesNo(EDMPortalConstants.CREATE_LEGAL_HOLDS);
        // legalHoldDataTable.fetchLegalholddata();
    } else if (oldValue
            .equalsIgnoreCase(EDMPortalConstants.ADD_TO_EXISTING)) {
        setLegalCreationBox(false);
        setLegalRemoveBox(false);
        setLegalRevertRemoval(false);
        setAddToExisting(true);
        setRadioYesNo(EDMPortalConstants.ADD_TO_EXISTING);
        // legalHoldDataTable.fetchLegalholddata();
    } else if (oldValue.equalsIgnoreCase(EDMPortalConstants.REVERT_REMOVAL)) {
        setLegalCreationBox(false);
        setLegalRemoveBox(false);
        setLegalRevertRemoval(true);
        setAddToExisting(false);
        setRadioYesNo(EDMPortalConstants.REVERT_REMOVAL);
        // legalHoldDataTable.fetchLegalholddata();
    } else {
        setLegalCreationBox(false);
        setLegalRemoveBox(true);
        setLegalRevertRemoval(false);
        setAddToExisting(false);
        setRadioYesNo(EDMPortalConstants.REVERT_REMOVAL);
        // legalHoldDataTable.fetchLegalholddata();
    }
}
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102

1 Answers1

0

Initialize the "selectedItem" field like in this example:

The facelet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
<head>
  <title>JSF Facelet</title>
</head>

<body>
  <h:form>
    <h:selectOneRadio value="#{myBean.selectedItem}">
      <f:selectItems value="#{myBean.items}"/>    
    </h:selectOneRadio>
  </h:form>
</body>

</html>

The controller:

package x;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named
@ViewScoped
public class MyBean implements Serializable
{
  private List<String> items;
  private String selectedItem;

  @PostConstruct
  public void init()
  {
      items = new ArrayList<>();
      selectedItem = "Item 1";
      items.add( selectedItem );
      items.add( "Item 2" );
      items.add( "Item 3" );
  }

  public String getSelectedItem() { return selectedItem; }
  public void setSelectedItem( String selectedItem_ ) { selectedItem = selectedItem_; }
  public List<String> getItems() { return items; }
}
The Bitman
  • 1,279
  • 1
  • 11
  • 25