DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Step-by-Step Guide to Creating a Calculator App With HTML and JS (With Factor Calculator Example)
  • Scrolling With Konva.js and React

Trending

  • Detecting Plan Regression in SQL Server Using Query Store
  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  • Identity in Action
  • Alternative Structured Concurrency
  1. DZone
  2. Coding
  3. Languages
  4. JSF Checkbox Styled Using HTML and CSS

JSF Checkbox Styled Using HTML and CSS

In this post, we use the common use case of checkboxes on a web page to learn how to use JSF with HTML and CSS. Let's get started!

By 
Mehdi Hafid user avatar
Mehdi Hafid
·
Oct. 03, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
11.2K Views

Join the DZone community and get the full member experience.

Join For Free

Want to style your checkboxes with HTML/CSS code? Want to change this:

Image title

to look like this?

Image title

First, let’s write code that uses JSF 2.2 to display a checkbox.

Here is the JSF code:

<h:body>

       <h:form prependId="false">

              <h:selectManyCheckbox value="#{dynamic.selectedOptions}" layout="pageDirection">

                     <f:selectItems value="#{dynamic.entities}" var="entity" itemLabel="#{entity.name}" itemValue="#{entity.id}" />

              </h:selectManyCheckbox>



              <h:commandButton value="Go" action="#{dynamic.rawJSF()}">

                     <f:ajax execute="@form" render="@form" />

              </h:commandButton>

       </h:form>

</h:body>

Here is the dynamic view scoped bean (javax.faces.view.ViewScoped)

@Named
@ViewScoped

public class Dynamic implements Serializable{
       private static final long serialVersionUID = 1L;

       private List<Entity> entities = new ArrayList<>();
       private String[] selectedOptions;

       public Dynamic() {
              Random random = new Random();
              for(int i = 0; i < 5; i++) {
                     Entity radio = new Entity();
                     radio.setId(i);
                     radio.setName("option " + (random.nextInt(999)));
                     entities.add(radio);
              }
       }

       public void rawJSF() {
              System.out.println("Printing selected options :");
              if(selectedOptions != null) {
                     for(String s : selectedOptions) {
                           System.out.println("Entity with ID : " + s + " is selected");
                     }
              }
       }

Getters and Setters for Entities and Selected Options

Running the code above will give us this:

Image title

We get the following messages printed out when we select three of the choices:

2018-06-30T21:23:09.976+0000|Info: Printing selected options :

2018-06-30T21:23:09.976+0000|Info: Entity with ID : 2 is selected

2018-06-30T21:23:09.976+0000|Info: Entity with ID : 4 is selected

Now, I'll be using the Metronic HTML template, which I purchased on Themeforest. Using this template, the checkboxes now look like this:

Image title

The HTML for the above checkboxes looks like this:

<div class="m-checkbox-list">
              <label class="m-checkbox">
                     <input type="checkbox"/>
                     Option 1
                     <span></span>
              </label>
              <label class="m-checkbox">
                     <input type="checkbox"/>
                     Option 2
                     <span></span>
              </label>
              <label class="m-checkbox">
                     <input type="checkbox" checked="checked"/>
                     Checked
                     <span></span>
              </label>
       </div>

Here is how to use the Metronic HTML/CSS code in JSF. First, I will show you the code then the I will explain it.

New JSF Page: CSS and JS Declaration Omited

New DynamicMetro View scoped bean:

<h:body >
       <h:form prependId="false" styleClass="m--margin-40">
              <div class="m-checkbox-list">
                     <ui:repeat var="entity" value="#{dynamicMetro.entities}" >
                           <label class="m-checkbox" for="checkbox#{entity.id}">
                                  <input id="checkbox#{entity.id}" type="checkbox"
                     jsf:value="#{dynamicMetro.oneChecked[entity.id]}"/>
                                  #{entity.name}
                                  <span></span>
                           </label>
                     </ui:repeat>
              </div>

              <h:commandButton value="Go" action="#{dynamicMetro.metronicJSF()}" styleClass="btn btn-warning">
                     <f:ajax execute="@form" render="@form" />
              </h:commandButton>
       </h:form>
@Named
@ViewScoped

public class DynamicMetro implements Serializable{
       private static final long serialVersionUID = 1L;

       private List<Entity> entities = new ArrayList<>();
       private Map<Integer, Boolean> oneChecked = new HashMap<>();      

       public DynamicMetro() {
              Random random = new Random();
              for(int i = 0; i < 5; i++) {
                     Entity radio = new Entity();
                     radio.setId(i);
                     radio.setName("option " + (random.nextInt(999)));
                     entities.add(radio);
                     oneChecked.put(radio.getId(), false);
              }
       }

       public void metronicJSF() {
              System.out.println("Printing selected options :");
              for(Entry<Integer, Boolean> entry : oneChecked.entrySet()) {
                     System.out.println("Entity with ID : " + entry.getKey() + " is selected ? " + entry.getValue());
              }
       }

Getters and Setters for Entities and the Checked Option

Select these options and press go:

Image title

This will print the following:

2018-06-30T22:29:04.766+0000|Info: Printing selected options :

2018-06-30T22:29:04.766+0000|Info: Entity with ID : 0 is selected ? true

2018-06-30T22:29:04.766+0000|Info: Entity with ID : 1 is selected ? false

2018-06-30T22:29:04.766+0000|Info: Entity with ID : 2 is selected ? true

2018-06-30T22:29:04.766+0000|Info: Entity with ID : 3 is selected ? false

2018-06-30T22:29:04.766+0000|Info: Entity with ID : 4 is selected ? false

The only thing here that needs explaining is the map, oneChecked. This map holds the id of the entity as the key and a boolean as its value.

The JSF part (jsf:value="#{dynamicMetro.oneChecked[entity.id]}" ) sets the value in the map for the corresponding entity if checked to true, otherwise it's false.

Bonus: Permitting Only One Option to Be Selected (Radio):

Update your JSF code:

<div jsf:id="selectAnswer" class="m-checkbox-list">
       <ui:repeat var="entity" value="#{dynamicMetro.entities}" >
              <label class="m-checkbox" for="checkbox#{entity.id}">
                     <input id="checkbox#{entity.id}" type="checkbox"
              jsf:value="#{dynamicMetro.oneChecked[entity.id]}"
              jsf:valueChangeListener="#{dynamicMetro.selectOnlyThis(entity.id)}">
                           <f:ajax event="click" execute="@this" render="selectAnswer" />
                     </input>
                     #{entity.name}
                     <span></span>
              </label>
       </ui:repeat>
</div>

The apply a new Java method:

public void selectOnlyThis(int id){
       boolean oldChecked = oneChecked.get(id);
       for(Entry<Integer, Boolean> entry : oneChecked.entrySet()){
              oneChecked.put(entry.getKey(), false);
       }
       oneChecked.put(id, oldChecked ? false : true );
}
HTML CSS

Opinions expressed by DZone contributors are their own.

Related

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Step-by-Step Guide to Creating a Calculator App With HTML and JS (With Factor Calculator Example)
  • Scrolling With Konva.js and React

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook