javahtmljsfselectonemenuselectmanycheckbox

JSF SelectManyCheckbox/SelectOneMenu example


I would like to make a little html page where you can select the operators you would like to use in your calculation in a checkbox and under the checkbox there is a dropdown list which has the above selected operators as content. Everytime you check/uncheck an operation, the selectable contents of the dropdown list should change. I want to make this with a ValueChangeListener but I have no idea how to implement a value change listener in this exercise.

Here is my index.html

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h:form>
        <h:selectManyCheckbox onchange = "submit()">
            <f:selectItems value = "#{selectManyCheckboxBean.list}" valueChangeListener = ""/>
        </h:selectManyCheckbox>

        <h:outputText id = "outputText" value = ""/>
        <h:selectOneMenu value = "">
            <f:selectItems value = ""/>
        </h:selectOneMenu>
    </h:form>
</h:body>

And here is my SelectManyCheckboxBean.java

@ManagedBean
@SessionScoped
public class SelectManyCheckboxBean {
    private List<SelectItem> list;

    public SelectManyCheckboxBean() {
        list = new ArrayList<>();

        list.add(new SelectItem('+'));
        list.add(new SelectItem('-'));
        list.add(new SelectItem('*'));
        list.add(new SelectItem('/'));
    }

    public List<SelectItem> getList() {
        return list;
    }

    public void operatorValueChange (ValueChangeEvent event) {

    }
}

Could anybody give me a solution or at least a code snippet for the value change listener? My main problem is that I don't know how to tell the value change listener that it should add the selected operators to a list when they have been checked in the checkboxes above.


Solution

  • Just set a selectOneMenu attribute value to #{selectManyCheckboxBean.list}. That's where your checked values are.