Faces Grid selecting a single Row

How to make a Java Faces Table select just one row.
 
Thanks to Sun Developer Support for this – except for they left out an iddy bit that would stoping working.  By the way Sun Developer Support is definitely worth the money.
 
1.Create a new Web Application project, name it Row, and enable the Visual Web JavaServer Faces framework
 
2.Drag and drop a table component and a button on to the visual designer.

3.From the Servers tab drag and drop Data Sources->Travel->Tables->Person on to the table component.

4.Right Click on the Table Component and Select Table Layout.
Click on the “New” button to add a new column.
Using “Up” button move the newly added column to the top of the list.
Remove the header text.
Select Radio Button for component type and remove the text in Value Expression Field.

5.Add the following code to the page bean:
 public String getCurrentRow() {
        return tableRowGroup1.getRowKey().getRowId();
    }
  
    public void setCurrentRow(int row) {
    }
  
    private Object lastSelected=”0″;
  
    public Object getRBSelected() {
        String sv = (String)radioButton1.getSelectedValue();
        return sv.equals(lastSelected) ? sv : null;
    }
  
    public void setRBSelected(Object selected){
        if (selected != null) {
            lastSelected = selected;
        }
    }

6. In either the Outline window or the Visual Designer, select radioButton1.

7. In the Data section for radioButton1, click the … button for the selected property. A dialog box appears.
Select Use Binding, Click the Bind to an Object tab, select  RBSelected, and click OK.
The application will now use the getRBSelected() and setRBSelected() methods to display and save user input for this component.
Now change the Name property to buttonGroup
8.In the Advanced section of the Properties sheet, click the … button for the selectedValue property.
Select Use Binding, Click the Bind to an Object tab, select  currentRow, and click OK.
The application will now use the getCurrentRow() method to return the selected value for this component.

9.Drag a static text component on to the visual designer and add the following code in a button action method:

public String button1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        String aRowId = (String)RadioButton.getSelected(
                “buttonGroup”);
        RowKey aRowKey = personDataProvider.getRowKey(aRowId);
      staticText1.setText(“You Selected ” +
                personDataProvider.getValue(“PERSON.NAME”,aRowKey));
        return null;
    }

10.Build and run the application.

Advertisements

About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on March 10, 2008, in Uncategorized and tagged , , , . Bookmark the permalink. 4 Comments.

  1. Thank you, buddy! Simple and efficient: java best pratices…

  2. Thanks for you nice comment Ricardo/Steve

  3. I am new to netbeans where is the pagebean you said add the following code to the page bean thank you in advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: