Adding a Checkbox to an ADF 11g Table

SQL, at least the standard SQL and the SQL that Oracle implements does not support Boolean datatypes. In the decades that SQL has been a standard or even taking into account the liberties technology companies take with standards, I would have expected Boolean variables to appear in databases a long time ago. Since they aren’t there, typically a Boolean attribute is represented by a number in the database.

Anyway, ADF is no exception to this rule of not liking Booleans. I would have thought that you could create a Boolean in an Entity, tie it to a Number field in the database, and then create everything as normal, but, at least in Tech Preview 3, it does not appear to be as simple as that.

So I took a hint from my buddy, Andre (check out his blog in the Blogroll to the right, it is full of great examples), and on his advice, checked out an Undocumented Sample from Steve Muench’s blog.

Here are the steps I took based on Steve’s 10.1.2 example to overcome this limitation in ADF 11g. Perhaps there is a better way, but I am not aware of it yet.

To demonstrate this, I added a Number type attribute to the Employee’s table in the default HR schema. I called it OrganDonor, since it’s always a good idea for companies to know which employee’s are organ donors ;-)

Create the Entity and View as you normally would. Open the view and add a transient attribute called “OrganDonorAsBoolean”.

Click on “Java” in the sidebar of the View, then click the little pencil in the top right to bring up the “Select Java Options” dialog.

Check off “Generate View Row Class” and “Include accessors”, then click “OK”.

Open the EmployeesViewrowImpl.java file it created.

Change the getter and setter for the AsBoolean attribute:

/**Gets the attribute value for the calculated attribute OrganDonorAsBoolean.
*/
public Boolean getOrganDonorAsBoolean() {
return (Boolean) (new Number(1)).equals(getOrganDonor());
}

/**Sets <code>value</code> as the attribute value for the calculated attribute OrganDonorAsBoolean.
*/
public void setOrganDonorAsBoolean(Boolean value) {
setOrganDonor((value ? new Number(1):new Number(0)));
}

Save everything (as always).

Next, create the JSPX page, make it a JSF page.

Open the DataControl that contains the EmployeesView1 view.

Drag the view onto the JSPX page, and select Tables -> ADF Table.

Select all the attributes you want displayed in the table, do NOT select the original number-based attribute. It will not let you select a “SelectBooleanCheckbox” as component at this point, so just just “Input Text”.

After it puts the form on the page, put a Commit and Rollback button on the page.

Finally, go to the source of the JSPX page and change the input text component for “OrganDonorAsBoolean” to this:

<af:selectBooleanCheckbox value=”#{row.OrganDonorAsBoolean}” />

Now run the page to test it and your check boxes should be working fine.

If I come across an easier way to do this, particularly, one without having to create a RowImpl class, I will post it here.

Share/Save/Bookmark

Related posts:

  1. Determining Selected Table In ADF Faces There are many niceties that JDeveloper/ADF provides, but as all...
  2. ADF 11g Master-Detail Part 2 This tutorial explains how to add some useful functionality to...
  3. Adding Sound to the FlashCard JavaFX Game I finally had a small amount of time to look...
  4. How To Create A Master-Detail Page in ADF 11g This article builds upon the last article in which we...
  5. How To Create An Editable List Page in ADF 11g For those of you wondering how my presentation turned out...

1 comment

  1. selectBooleanCheckbox en tablas « Fetishcode…Thinking in objects

Leave a reply

You must be logged in to post a comment.