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

  • A boolean control
  • An object value holder, which can be other than a boolean type  (behaves as a non boolean control)

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.

  • Create a project (call it NonBooleanCheckbox).
  • Add a Table component from the Palette.
  • Assume you have already connected to the derby database, drag and drop the "person" table on to the table component.
  • Right clcik on the table component and bring up the table layout dialog.
  • Selected the FrequentFlyer column and change its component to checkbox
    (Note: if this column is of type boolean, checkbox will be automatically selected for you)
  • Select the source Page1.java from the project node and add a property of type boolean (call it frequentFlyer). Following code will be generated.
    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));
        };
    } 
  • Select the checkbox (table->tableColumn4->checkbox1) and click on the [...] of the selected property to bring up the custom property editor. In the dialog select Page1 -> frequentFlyer and click ok. Now the checkbox is bound to the frequemtFlyer property.
  • Deploy the application. You should see in the frequent flyer column, the rows with value "1" is selected and with "0" are unselected. (See Picture below)

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.

  • Add another Table component from the Palette
  • Drag and drop the "person" table on to the table component. Select to use the same CachedRowset when asked.
  • Right clcik on the table component and bring up the table layout dialog.
  • Selected the JobTitle column and change its component to checkbox
  • Select the selectedValue property of the checkbox in the propertysheet and type the value as "VP"
    Note: If the column type is not a string but an integer then you need to set the value in the Java source as
    public void prerender() {
        checkbox2.setSelectedValue(new Integer(20));
    }
  • Deploy the application. You should see in the job title column, the rows with value "VP" is selected and rest are unselected. (See Picture below)

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.

Comments:

Hi All, I used the tip "Displaying two dimensional array in the table component " in that I can able bind the data provider and the table component displaying the two dimensional array component. But at the execution the browser displaying the table as "No Items Found". Screen shot given below. Kindly, provide me the solution...

Posted by Kalki on April 10, 2006 at 06:05 PM PDT #

Hello winston, i am facinf a situation with checkboxes in tables mine is i am creting a shopping cart table whereby users can select items using the check box before i show the items in another dynamically created table in another page , i aslo have in my shopping cart page a cartegory combo box where my users can select a cartegory and view all the items under them but each time i cahne the cartegory the previous selection seems to be lost , how can i get all the selected items and use them to dynamically create a showcart page.Please your help would be needed

Posted by Iyenemi I Tyger on April 10, 2006 at 10:20 PM PDT #

Hi, I am using ur tip " How to create single selectable row Table component" In that I changed Trip Id column type static text to Radio button component. How do I get a selected cell value (I get cell value by specifying rowkey, fieldkey i.e column name). My question is How do I get a selected column value (Trip ID) using javascript. I can able to get the text field value using javascript. But how do I use the value in java page. I need to get the value of radiobutton using javascript. And one more doubt, is it possible to fetch values of ui component using script?

Posted by Ashokkumar on April 19, 2006 at 07:20 PM PDT #

hi. sorry for comment here. but i remenber a article about how to add a buttom to title of a table compoment. for example a buttom to save data. i wander if you could help me to find it. thank's alex

Posted by alex on April 20, 2006 at 12:16 AM PDT #

Thanks - this helped a GREAT deal! Godspeed.

Posted by Marthinus on February 19, 2007 at 03:24 PM PST #

Winston, thank you for the note. But I am looking at mapping an int to a checkbox without that extra step. My object has a field "int flyer;" and i really don't want to wrap my object, as there are MANY int fields which are really ONE(true)|ZERO(false). Any thoughts?

Posted by Richard Friedman on March 27, 2007 at 09:49 AM PDT #

How do we identify the rows selected in a table using checkbox in JSF .Please send me sample code

Posted by Meena on May 02, 2007 at 10:26 PM PDT #

I'm creating a dynamic table and I don't know how many columns it has. The table is the rowName and every other column is a checkbox. As I don't know the number of columns and I'm taking the data from an object I've designed the object to display in the data as a String property with the name and a boolean[] property. The problem is I don't know how to bind the checkbox1 column with the boolean[0] value and so on. thanks in advance. Azeroth

Posted by Azeroth on June 14, 2007 at 05:13 AM PDT #

balls

Posted by guest on September 17, 2007 at 10:09 PM 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 05:58 AM 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 03: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 01: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 04:59 PM PDT #

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 09:51 AM 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 03:08 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today