SelectOneRadio is used to choose a single item from a collection.
<style type="text/css">
.grid-radio td {
padding: .5rem;
}
</style>
<div class="card">
<h:form>
<h5 class="p-mt-0">Basic</h5>
<p:selectOneRadio id="console" value="#{radioView.console}" unselectable="true">
<f:selectItem itemLabel="Option1" itemValue="Option1"/>
<f:selectItem itemLabel="Option2" itemValue="Option2"/>
<f:selectItem itemLabel="Option3" itemValue="Option3"/>
</p:selectOneRadio>
<h5>Grid</h5>
<p:selectOneRadio id="city" value="#{radioView.city}" layout="grid" columns="3" styleClass="grid-radio">
<f:selectItems value="#{radioView.cities}" var="c" itemLabel="#{city}" itemValue="#{city}"/>
</p:selectOneRadio>
<h5>Responsive</h5>
<p:selectOneRadio id="city2" value="#{radioView.city2}" layout="responsive" columns="3">
<f:selectItems value="#{radioView.cities}" var="c" itemLabel="#{city}" itemValue="#{city}"/>
</p:selectOneRadio>
<h5>Custom Layout</h5>
<p:outputPanel id="customPanel" style="margin-bottom:10px">
<p:selectOneRadio id="customRadio" value="#{radioView.color}" layout="custom">
<f:selectItem itemLabel="Red" itemValue="Red"/>
<f:selectItem itemLabel="Green" itemValue="Green"/>
<f:selectItem itemLabel="Blue" itemValue="Blue"/>
</p:selectOneRadio>
<h:panelGrid columns="3" cellpadding="5">
<p:radioButton id="opt1" for="customRadio" itemIndex="0"/>
<h:outputLabel for="opt1" value="Red"/>
<p:spinner/>
<p:radioButton id="opt2" for="customRadio" itemIndex="1"/>
<h:outputLabel for="opt2" value="Green"/>
<p:inputText/>
<p:radioButton id="opt3" for="customRadio" itemIndex="2"/>
<h:outputLabel for="opt3" value="Blue"/>
<p:calendar/>
</h:panelGrid>
</p:outputPanel>
</h:form>
</div>
@Named
@RequestScoped
public class RadioView {
private String console;
private String city;
private String city2;
private List<String> cities;
private String color;
@PostConstruct
public void init() {
cities = new ArrayList<String>();
cities.add("Miami");
cities.add("London");
cities.add("Paris");
cities.add("Istanbul");
cities.add("Berlin");
cities.add("Barcelona");
cities.add("Rome");
cities.add("Brasilia");
cities.add("Amsterdam");
}
public String getConsole() {
return console;
}
public void setConsole(String console) {
this.console = console;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCity2() {
return city2;
}
public void setCity2(String city2) {
this.city2 = city2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public List<String> getCities() {
return cities;
}
}