How to bind checkbox to non-boolean datatype?

CheckBox has two properties -- selected and SelectedValue

selected property is used purely for display purpose i.e, selecting/deselecting the checkbox being displayed

selectedValue is returned by checkbox1.getSelected(), when the checkbox is selected i.e, checkbox1.isSelected() returned true.

checkbox works intuitively/easily when bound to a [Bb]oolean data type i.e, w/o additional wiring/coding.

When it needs to be bound to a non-boolean datatype (like Integer, or String), we need to explicitly create a boolean property and bind the checkbox to that property. This property should have getter (isPropertyname) and setter (setPropertyname).

Suppose that you have TRAVEL.PERSON.FrequentFlyer is of type SMALLINT. So,

0. Drop a table and drop this database table TRAVEL.PERSON onto it (which has FrequestFlyer as smallint)

1. we add/create a property, named frequentFlyer, in page with type\* boolean\* (use primitive type boolean and not Object type Boolean) i.e, Right click on $ProjectNode in Projects> Source Pakcages> $DefaultPackage> Page1.java. Choose Add> Property. Enter name as frequentFlyer, type as _boolean_ and click OK

2. Modify it's gettter (in Page1.java) as in: 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; }

3. Modify it's setter as in :

public void setFrequentFlyer(boolean frequentFlyer) { if(frequentFlyer){ setValue("#{currentRow.value['PERSON.FREQUENTFLYER']}", new Integer(1)); }else{ setValue("#{currentRow.value['PERSON.FREQUENTFLYER']}", new Integer(0)); }; }

4. Modify the table layout to display FrequentFlyer column as checkbox and bind the selected property of the checkbox (using property bindings) to this FrequentFlyer boolean property in the PageBean and Run Project to see it working!

These little extra steps are needed to bind checkbox to a non-boolean type database column.

Comments:

Post a Comment:
Comments are closed for this entry.
About

sakthi

Search

Archives
« July 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
31
  
       
Today