Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Binding Dynamic Multi-select Checkboxes with JSF

DZone's Guide to

Binding Dynamic Multi-select Checkboxes with JSF

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

This tutorial looks at how JSF makes it easy to create a dynamic list of checkboxes that can be used to select values on the server.

Given a list of strings, we want to display the list of values, each with a check box and be able to select or deselect that item from inclusion in the selected set. We want to do it without having to manually add each checkbox to the page and bind it to a property on the backing bean.

JSF controls can be bound to a value in a map using the syntax #{some.map['someKey']} where someKey can also be an EL expression. This means we can bind a checkbox to a Boolean value in the map. If we have an iterable list of of key values, we can bind multiple checkboxes to different values in a map.

  1. Create a new backing bean called FormBean with the following code :
    public class FormBean {

    private List<String> items;

    private Map<String,Boolean> checkMap = new HashMap<String,Boolean>();

    public FormBean() {
    items = new ArrayList<String>();
    items.add("Open");
    items.add("Closed");
    items.add("Pending");
    items.add("Suspended");
    items.add("In Progress");
    items.add("Rejected");

    //fill the check map with the items defaulted to unchecked
    for (String item : items) {
    checkMap.put(item,Boolean.FALSE);
    }
    }

    public List<String> getItems() {
    return items;
    }

    public Map<String, Boolean> getCheckMap() {
    return checkMap;
    }

    }
    This creates our list of items and populates it with a set of values. The checkMap will be used to track which items are selected and which are not. The map contains a set of String,Boolean pairs.
  2. Add one more method to return a string telling us which items are selected
    public String getSelected() {
    String result = "";
    for (Entry<String,Boolean> entry : checkMap.entrySet()) {
    if (entry.getValue()) {
    result = result + ", "+entry.getKey();
    }
    }
    return result.length() == 0 ? "" : result.sub string(2);
    }
    To get the list of selected items, we iterate through the set of entries and if the value in the map is true, then it is selected and added to the result string.
  3. Our backing bean is complete, now we will create our view
    <h:form>
    <h:outputText value="Currently Selected : #{formBean.selected}" />

    <ui:repeat var="item" value="#{formBean.items}">
    <h:outputText value="#{item}" />
    <h:selectBooleanCheckbox value="#{formBean.checkMap[item]}"/>
    </ui:repeat>

    <h:commandButton value="Update" action="refresh" />
    </h:form>
    We iterate through the list of items and for each one, display the caption (the item itself) and a checkbox bound to the Boolean checkMap entry for that item.

Unfortunately, we can’t iterate through the keySet because JSF doesn’t provide for iteration over Sets (next version JSF Expert Group!) so we have to use a separate list as our key. However, this is a nice way of providing a dynamic set of checkbox controls that are bound to dynamic values without having to use any tricks to keep track of control bindings.

If you want to make the list update when you click an item, just add the f:ajax tag to the checkbox like so :

<h:selectBooleanCheckbox value="#{formBean.checkMap[item]}">
<f:ajax event="click" execute="@form" render="@form" />
</h:selectBooleanCheckbox>

You can download the working code for this example (JsfCheckMap Demo Source Code) which you can run by unzipping it and in a command line running mvn jetty:run or windows users can just double click run.bat.

From http://www.andygibson.net/blog/tutorial/binding-dynamic-multi-select-checkboxes-with-jsf/

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}