Creator Tip: Understanding the checkbox component to display non boolean value
Some database such as Apache Derby does not support boolean type. According to JDBC documentation (see Table 8.7-JDBC Types Mapped to Database-specific SQL Types), the JDBC type BIT is mapped to the Java type boolean. If you look at the supported data types for derby, BIT (boolean) is not listed. In such case SMALLINT (1 or 0) is used to represent boolean type. However, SMALLINT does not map directly to boolean but to the Java type short. If the user wants to bind this column (SMALLINT) of the database table to a checkbox component, the actual status of the columns will not be reflected in the displayed table component. i.e column row with 1's will not be displayed as selected checkbox. This tip explains how to work around this and also explain about how checkbox component can be used to display any type of column. You can download this tip project from here.
First we need to understand the basic checkbox component. The bundled basic checkbox component is a versatile component. It can handle more than just boolean value. It can behave as
Checkbox as a boolean control
If the database column is of type boolean, then this is a non brainer. Bind the checkbox to the database column directly and the checkbox would display the status as checked for the value true and unchecked for the value false. However, this is problematic if you are using database that does not support boolean type and you are using a SMALLINT to represent the true (1) and false (0) state. In this case you need to use the following work around.
Assume in the bundled database, the table "DataSources -> Travel -> Person" has a column "FrequestFlyer" which is of type SMALLINT (derby database connection). If the person is a frequent flyer, then the value of the column is 1, else 0. We want to display this column as a checkbox. In order to achieve this follow the below steps.
public boolean isFrequentFlyer() {
Object value = getValue("#{currentRow.value['PERSON.FREQUENTFLYER']}");
if(value != null){
Integer freqFlyer = (Integer) value;
return freqFlyer.intValue()==1? true : false ;
}
return false;
}
public void setFrequentFlyer(boolean frequentFlyer) {
if(frequentFlyer){
setValue("#{currentRow.value['PERSON.FREQUENTFLYER']}", new Integer(1));
}else{
setValue("#{currentRow.value['PERSON.FREQUENTFLYER']}", new Integer(0));
};
}
Checkbox as a non boolean control
The checkbox component has two attributes selected and selectedValue. When used as a boolean control the selectedValue attribute is not used. The value of the selected attribute is set either as true or false. When used as a non boolean control both selected and selectedValue are used. When the value of the selected property is equal to the value of the selectedValue property, the checkbox is in a selected state and a checkmark is displayed.
Let us see an example. Assume you want to display a selected checkbox if the job title of the person is VP. Follow the below steps.
public void prerender() {
checkbox2.setSelectedValue(new Integer(20));
}
What happens when the user click on the checkbox to select it. In this case the value of the selected property is assigned the value of the selectedValue property. However, if you deselect the checkbox the selected property is set to null and if you update, you might loose your original values in the database. So in this scenario, my recommendation is to use a read only (disabled) checkbox, just to display the state. Use the method I discussed in the section "Checkbox as a boolean control", if you need to update the non boolean columns.
Posted at 02:01PM Apr 07, 2006 | Permanent link to this entry
Posted by Kalki on April 11, 2006 at 01:05 AM PDT #
Posted by Iyenemi I Tyger on April 11, 2006 at 05:20 AM PDT #
Posted by Ashokkumar on April 20, 2006 at 02:20 AM PDT #
Posted by alex on April 20, 2006 at 07:16 AM PDT #
Posted by Marthinus on February 19, 2007 at 11:24 PM PST #
Posted by Richard Friedman on March 27, 2007 at 04:49 PM PDT #
Posted by Meena on May 03, 2007 at 05:26 AM PDT #
Posted by Azeroth on June 14, 2007 at 12:13 PM PDT #
balls
Posted by 62.189.54.51 on September 18, 2007 at 05:09 AM PDT #
How can I to put two checkboxes, each one in differents column, to indicate diferets states?.
I can put one column with checkbox, but I dont know put two columns with check box each one. Thanks.
Posted by Liliana on October 19, 2007 at 12:58 PM PDT #
Hi Winston. First of all I would like to thank you cause this tutorial was very helpful to me. But the thing is that when I run the example, this code getValue("#{currentRow.value['PERSON.FREQUENTFLYER']}"); throws an PropertyNotFoundException.What could I possibly doing wrong?
Thanks in advance,
Wadi
Posted by Wadi on April 29, 2008 at 10:41 AM PDT #
Hi Wadi, you need to check if the dataprovider you bind to the table actually contain the column PERSON.FREQUENTFLYER. Right click on the table, bring the table layout and see if the selected data provider has the column.
Posted by Winston Prakash on April 29, 2008 at 08:37 PM PDT #
Hi ,
I am working on JSF
I have one problem in JSF checkbox sample,
I have an excel sheet and i read excel sheet each row as a bean and add that bean to a list and displaying a list in data table along with a check box in each row.
means user can make select items in excel sheet or required..
I am unable to select a check box with that element is added to the list..
can any one help
Thanks in advance
regards,
Sureshv
Posted by suresh on July 06, 2008 at 11:59 PM PDT #
http://www.batteryfast.co.uk/ibm/thinkpad-x40.htm ibm thinkpad x40 battery,
http://www.batteryfast.co.uk/ibm/thinkpad-x60.htm ibm thinkpad x60 battery,
http://www.batteryfast.co.uk/ibm/thinkpad-r32.htm ibm thinkpad r32 battery,
Posted by laptop batteries on October 24, 2008 at 12:11 AM PDT #
k;lk
Posted by 203.91.193.50 on November 10, 2008 at 02:23 AM PST #
Thank you.
Could you tell me how to select tow or three type of a non boolean value at the same time? Sorry for my poor english.
Posted by MAXUJUN on December 03, 2008 at 05:51 PM PST #
Hi
I have a Problem , I got the tutorial on Checkbox as a boolean control.
But My problem is, i want to update the record of the Table along with the check box current status.
Suppose if i unchecked any of the checkbox then record is updated with new value.
Posted by Mayank on December 23, 2008 at 11:08 PM PST #