Fahim Parkar
Fahim Parkar

Reputation: 31627

Single Select Checkbox Using JSF

I am creating registration form in JSF and I have field as Active, Inactive where I need to use check boxes and these checkboxes should be SINGLE SELECT ONLY. (Note: I can use radio button and solve the problem, however due to some problem I have to use check boxes).

Please suggest me how to get this done?

Thanks in advance...

Upvotes: 0

Views: 10490

Answers (1)

Mr.J4mes
Mr.J4mes

Reputation: 9266

I think what you need is <h:selectBooleanCheckbox>. The value binded to this component can only be either true (Active) or false (Inactive). It will be something like the following:

<h:selectBooleanCheckbox id="active" value="#{mrBean.active}" >
   <f:ajax render="inactive" listener="#{mrBean.onActiveStatusChange}" />
</h:selectBooleanCheckbox>
<h:selectBooleanCheckbox id="inactive" value="#{mrBean.inactive}" >
   <f:ajax render="active" listener="#{mrBean.onInactiveStatusChange}" />
</h:selectBooleanCheckbox>

@ManagedBean
@RequestScoped
public class MrBean {
   private boolean active;
   private boolean inactive;

   @PostConstruct
   public void prepareMrBean() {
      this.active = true;
   }

   public void onActiveStatusChange() {
      if (active) inactive = false;
   }

   public void onInactiveStatusChange() {
      if (inactive) active = false;
   }
}

Upvotes: 1

Related Questions