jsfprimefacesjsf-2.2selectoneradio

Using an image as a label of p:selectOneRadio


Given the following list of <p:selectOneRadio>.

<p:selectOneRadio layout="grid" columns="1" value="#{1}">
    <f:selectItem id="paypal" itemValue="1" itemLabel="Paypal"/>
    <f:selectItem id="wireTransfer" itemValue="2" itemLabel="Wire Transfer"/>
</p:selectOneRadio>

Is it possible to display an image as a label of <f:selectItem> so that the list looks something like the following?

enter image description here

I tried using a <p:outputLabel> placing a <p:graphicImage> inside and removing the itemLabel attribute from the<f:selectItem> inside the <p:selectOneRadio> but either way it did not work.

<p:outputLabel for="paypal">
    <p:graphicImage library="default" name="images/payments/paypal_logo.jpeg"/>
</p:outputLabel>

Solution

  • You can solve it using the SelectOneRadio - Custom Layout

    Here an example:

    <h3>Custom Layout</h3>
    <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>