Using Checkboxes in a Table Component

Here is a one way to add a checkbox column to a table and determine which checkboxes have been checked.

  1. Add a property named selectedRows of type java.util.Set to SessionBean1.

  2. Add the following code to the init() property in SessionBean1.
    
    selectedRows = new HashSet();
    
    
  3. Add a property named selected of type boolean to SessionBean1.

  4. Change the getter and setter to the following code:
    
       /\*\*
         \* Getter for property selected
         \* @return true if checkbox in current row is selected
         \*/
        public boolean isSelected() {
            TableRowDataProvider rowData = (TableRowDataProvider) getBean("currentRow");
            if (rowData == null) {
                return false;
            } else {
                String rowId = rowData.getTableRow().getRowId();
                if (this.getSelectedRows().contains(rowId)) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        
        /\*\*
         \* Setter for property selected.
         \* @param boolean; true if checkbox is selected, false if checkbox is cleared.
         \*/
        public void setSelected(boolean b) {
            TableRowDataProvider rowData = (TableRowDataProvider) getBean("currentRow");
            if (rowData != null) {
                String rowId = rowData.getTableRow().getRowId();
                if (b) {
                    this.getSelectedRows().add(rowId);
                } else {
                    this.getSelectedRows().remove(rowId);
                }
            }
        }
    
    
  5. Bind the Checkbox component's selected property to SessionBean1 > selected.

  6. Use code like the following to determine which checkboxes are selected.
    
    Iterator rows = getSessionBean1().getSelectedRows().iterator();
    while (rows.hasNext()) {
        String rowId = (String) rows.next();
        RowKey rowKey = <your table's data provider>.getRowKey(rowId);
        ... use the row key to access the data in the data provider
    }
    
    
Comments:

While this is a great way to maintain the state of selected checkbox post-submit, is there anyway we can marry this tutorial as well as Winston's tutorial on checkboxes (http://blogs.sun.com/roller/page/winston?entry=multi_column_listbox_table)? Currently, my only issue is that the "highlight" from selecting the checkbox is lost after the submit. I know in an earlier tutorial by Winston he did something similar with radio buttons, and used a TableSelectPhaseListener to handle the hashing/storage. Might that solve this problem? Anyway, any tips on this would be great.

Posted by J Jones on March 31, 2006 at 03:03 AM PST #

Yes, are working on a full table component tutorial.

Posted by guest on April 03, 2006 at 03:05 AM PDT #

How can I do the same by using ObjectListDataProvider rather than TableRowDataProvider

Posted by Luis Flores on November 21, 2007 at 07:56 AM PST #

Here are some related blog entries that might answer your question:

http://blogs.sun.com/divas/entry/using_the_object_array_data

http://blogs.sun.com/divas/entry/table_component_sample_visual_web

Also, here is a section in a VWP tutorial that covers object list data providers:

http://www.netbeans.org/kb/55/vwp-hibernate.html#object

Hope this helps,

Chris

Posted by Diva #2 on November 21, 2007 at 08:18 AM PST #

Hi all,

I use this tip but I want now a single selectable row table as the one Winston did with radioButton and Tablephaselistener. Is there someone who already achieved the same things with this tip? Please help!

Posted by Miele on July 04, 2008 at 01:41 AM PDT #

Miele,

I used the table phase listener in http://blogs.sun.com/divas/entry/table_component_sample_visual_web

Although that table uses checkboxes instead of radio buttons, you can see a working example of the use of a table phase listener. You need to download and unzip the project to see the full details.

You can learn the details about using radio buttons at http://www.netbeans.org/kb/55/vwp-about_components.html. In particular, "If there are several Radio Button components with the same value for the name property, only one radio button in the set can be selected at a time. You typically use the name property when you have a Radio Button component in a Table Component column."

Posted by diva#2 on July 05, 2008 at 03:04 AM PDT #

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

divas

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