VwpFAQCheckboxInTable

How can I determine which checkbox has been checked in a table?

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
      }
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo