sameer
sameer

Reputation: 220

JSF 2.0 and checkboxes

am new to this so can you all try to help me please.

In my java class,

public String mycheckbox;
// setters and getters for mycheckbox

In my form,

<input id="f2_chk_1" type="checkbox" value="1" name="f2_chk_1">
1
<br>
<input id="f2_chk_2" type="checkbox" value="2" name="f2_chk_2">
2
<br>
<input id="f2_chk_3" type="checkbox" value="3" name="f2_chk_3">
3
<br>
<input id="f2_chk_4" type="checkbox" value="4" name="f2_chk_4">
4
<br>
<input id="f2_chk_5" type="checkbox" value="5" name="f2_chk_5">
5
<br>
<input id="f2_chk_6" type="checkbox" value="6" name="f2_chk_6">
MT
<br>
<input id="f2_chk_7" type="checkbox" value="7" name="f2_chk_7">
6

What i want is when i check a checkbox, mycheckbox should automatically get the value; even multiple checkboxes are checked.

I googled it but came up with examples where the checkboxes are defined in the java class itself and things which are not meaningful to me.

Upvotes: 1

Views: 4550

Answers (3)

BalusC
BalusC

Reputation: 1108632

You can use the <h:selectManyCheckbox> component for this with a List<String> as available values and another List<String> as checked values. Here's a complete kickoff example which achieves your initial requirement:

View:

<h:form>
    <h:selectManyCheckbox value="#{bean.checkedValues}" layout="pageDirection">
        <f:selectItems value="#{bean.availableValues}" />
    </h:selectManyCheckbox>
    <h:commandButton value="submit" action="#{bean.submit}" />
</h:form>

Bean:

@ManagedBean
@ViewScoped
public class Bean {

    private List<String> checkedValues;
    private List<String> availableValues;

    @PostConstruct
    public void init() {
        availableValues = new ArrayList<String>();
        availableValues.add("1");
        availableValues.add("2");
        availableValues.add("3");
        // ...
    }

    public void submit() {
        System.out.println("Checked values: " + checkedValues);
    }

    public List<String> getCheckedValues() {
        return checkedValues;
    }

    public void setCheckedValues(List<String> checkedValues) {
        this.checkedValues = checkedValues;
    }

    public List<String> getAvailableValues() {
        return availableValues;
    }

}

Upvotes: 2

roel
roel

Reputation: 2003

Aba Dov link is rather good I think. I don't understand wy you need something better. But you can define a variabel list and let that list render the checkboxes.

in your bean

private ArrayList<SelectItem> items;

public ArrayList<SelectItem> getItems(){
   items = new ArrayList<SelectItem>();
   items.add(new SelectItem(<value>, <description>);
    ...
   items.add(new SelectItem(<value>, <description>);
   return items
}

and in your jsf page

<h:selectManyCheckbox value="#{<yourbean>.checkboxvalue}">
<f:selectItems value="#{<yourbean>.items}" />
<h:selectManyCheckbox />

Upvotes: 0

Aba Dov
Aba Dov

Reputation: 962

First, You should change the type of the variable in your java class to boolean

public boolean myCheckbox;

You can view a full coded example here mkyong checkbox jsf-2

Second, inorder to select one checkbox when others are checked, you can use javascript and act after the checkbox is checked\unchecked

<h:selectBooleanCheckbox value="#{Class.Varible}" onselect="checkCheckbox()" />

and create a js function that implements the business logic.

function checkCheckbox(){

  // if at least one checkbox is checked, check specific checkbox


}

You should consider using selectManyCheckbox.

Hope this helps

Upvotes: 1

Related Questions