Visual Web Pack Tip: Dynamic Table Field CheckBox for None Boolean Column Types

Having been working with Visual Web Pack for a while and never managing find the answer to my particular issue of getting a CheckBox to work within a  Table mapped to a database I finally  spent sometime really investigating this and  have a simple solution that works for View / Insert / Update. As a result I decided that it would be a good idea to place this where it is easily accessible.

Resources
Creating the Page

My requirement was to create a page the displayed the data held within a Derby Database in s simple Filtered Table format. A number of the columns on the Database held Boolean information and because Derby does not support the Boolean type they were stored as CHAR(1) with a values of "Y" or "N" but at display time I required a simple CheckBox; as shown below.

Visual Web Pack Table

Designing the Page
  1. Create a new Visual Web Pack Application
  2. From the Palette Drag a Message Group on the the Canvas.
  3. Drag a Table onto canvas below the Message Group
Configuring the Table
  1. Drag the TOX_CLASSIFICATION Table from the Services->Databases->Toxophily->Table and drop it onto the Visual Web PAck Table component within the Designer.
  2. Right-Click on the Table and choose Table Layout from the Popup Menu.
    The Dialog will show all the fields available within the automatically created DataProvider.

    TableLayout

  3. Deselect the Metric, Gents and Create_Date fields and add two new columns. Move them to the top of the Selected list.
  4. Change the Component type to Checkbox
  5. Give the the Titles Metric and Gents Respectively.
  6. At this point the Value Expression is unimportant.
We now have a design screen similar to the one below. By default all the types will be Static Text. In my screens below I have change a number of them to Drop Downs and TextFields to allow me to edit the values. In addition I have added another table column that I have made into a button. These are not necessary to show the point of this blog.

Design View

Adding CheckBox Properties

We now need to define the Property Accessors that will be linked to the CheckBoxes allowing their values to be changed dynamically. To do this will will need to switch to the Java view for the Page and add the following Code.

    private boolean gents = true;

    public boolean isGents() {
        return "Y".equals((String) getValue("#{currentRow.value['TOX_CLASSIFICATION.GENTS']}"));
    }

    public void setGents(boolean gents) {
        this.gents = gents;
        setValue("#{currentRow.value['TOX_CLASSIFICATION.GENTS']}", gents ? "Y" : "N");
    }

    private boolean metric = false;


    public boolean isMetric() {
        return "Y".equals((String) getValue("#{currentRow.value['TOX_CLASSIFICATION.METRIC']}"));
    }

    public void setMetric(boolean metric) {
        this.metric = metric;
        setValue("#{currentRow.value['TOX_CLASSIFICATION.METRIC']}", metric ? "Y" : "N");
    }

Once we have linked the Table CheckBoxes to the appropriate Property displaying data within the Table will cause the above methods to be accessed multiple times once for each row displayed. On each invocation the code highlighted in Green will return a value specific to that row because the value of currentRow will evaluate to the row currently being accessed for display. When the CheckBox is changed the code highlighted in Red will be executed and because the action is fired for a specific Row the currentRow variable will point to the record in the underlying data set that matches the display value thus setting the value within the underlying data set.

Configuring the CheckBoxes
  1. Switch back to the design view
  2. Select the first occurrence of the CheckBox within the Table Column.
  3. Select the Ellipsis next to the Selected Property to display the Dialog

    Configure CheckBox

  4. Select Use Binding and then select the Property that matches the CheckBox you are configuring.
  5. Select Ok and repeat for the other CheckBox.
Testing The Application

If you now select run for the project you will see the Page at the top of this blog, or one similar, and you should see that the CheckBoxes are either Check / Unchecked dependent on the data within the table.

The attached resource contains additional code for implementing Add / Update / Delete functionality and a Filtered Table based on the FilteredTableDataProvider (which has some peculiarities) but I will cover these in a later blog. For now this is a simple example for creating the CheckBoxes linked to none boolean columns.

Comments:

The image of your table component with the multiple filter drop-downs is fantastic! This is the concept that I have been researching for months now. Please tell me where I can access the filter functionality using NB6.1, with the Woodstock components that ship with it.

Secondly, I cannot find the TOX_CLASSIFICATION Table in my default NB6.1, so the Visual Designer opens with the component not found error. Do you have any tips how I can obtain the table schema?

Excellent Blog!
Dave

Posted by David on July 05, 2008 at 12:08 PM GMT #

David,

The TOX_CLASSIFICATION table definition cal be found in the src/sql/Toxophily.sql. To create the Filter Bar at the top you will need to do a little bit of coding directly in the JSP but the majority can be done as follows:

1) Drag a Group Panel component onto the canvas (anywhere we will move it later).
2) Drag the various Filter Drop Downs / Label etc into the Group Component.
3) Switch to the JSP view and then just after the <webuijsf:table tag add the following:

<f:facet name="actionsTop">
</f:facet>

4) Now cut and paster the JSP code that is associated with the Group Panel previously created between the facet tags and remove the style attribute of the Group Panel (this will remove the fixed positioning information)
5) Switch back to Design view and it should look something like my example.

In addition you will need to create "Filter Table Data Provider" (from the Advanced Data Providers) and attach the table to this configuring as appropriate. If you look at the code/properties in the download you should be ok.

You may notice that there are two tables connected to the same Cached RowSet, although one is invisible, and the reason for this can be found here http://www.netbeans.org/issues/show_bug.cgi?id=137127

Posted by guest on July 07, 2008 at 02:24 AM GMT #

The downloaded application works flawlessly! My amusing story: after re-creating the tables, I had to manually change mappings to point to my local database. I spent a whole day on this, at the end of the day, NetBeans did not recognize where my database was, I was convinced it was a firewall issue somewhere on my PC. Frustrated, I closed everything down until tonight, and tackle it again. NetBeans immediately built and deployed everything like there was never any problem. Go figure. Thank you for posting your work, saving me uncounted hours of research time. Best regards! David

Posted by David on July 10, 2008 at 06:38 PM GMT #

Please!! I don't find any examples about FilteredTableDataProvider use. I need help because I only get NullPointerException with that component, using it in the same way that CachedRowSetDataProvider. Heeeeeeeeeeelp!!!!!!

Posted by Santiago Santiago González on December 17, 2008 at 12:24 AM GMT #

Ok this entry does use the FilteredTableDataProvider so if you download the project and take a look you will see in detail how this is done. Essentially, because of an issue with the Data Provider which I have a case open for, if you try and use it directly it will throw an Exception. The way I worked around this was to have two Tables on the page the first using a normal DataProvider mapped to the CachedRowSet and the second using the FilteredDataProvider over the same CachedRowSet. I then set the properties on the first table so that it is not displayed. The net result of this is that the first Table forces the RowSet to query the Data and then the Filtered Tables DataProvider works on this result. This is a work around until they resolve the FilteredDataProvider issue.

Posted by guest on December 17, 2008 at 08:20 AM GMT #

Thank you very much 12.9.145.82!!!!! At least, I can to see that there is an intern problem with FilteredTableDataProvider. I would take your tip into account. Thanks!!

Posted by Santiago Santiago González on December 18, 2008 at 12:24 AM GMT #

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

As a member of the Oracle A-Team we specialise in enabling and supporting the Oracle Fusion Middleware communities.

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
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