Radio Buttons in Tables

Here is a quick hack for detecting what radio button was clicked in a Table component.

Use the Table Layout dialog to add a column to the table and set its Compnent Type be Radio Button.

Add this 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;
        }
    }

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

In the Properties window for radioButton1, set the name property (in the Advanced section) to buttonGroup.

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.

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.

Now to find out what as been selected. In this example, the code is in a button action method:

        String aRowId = (String)RadioButton.getSelected(
                "buttonGroup");
        RowKey aRowKey = stateDataProvider.getRowKey(aRowId);
        selectedStateStaticText.setText("You Selected " +
                stateDataProvider.getValue("STATE.STATENAME",aRowKey));

If you want the selection to remain for when the user returns to the page, then you can stick the properties in a session bean. But, then you need to consider what to do if the selected row doesn't exist anymore.

Comments:

Hi and thanx for this blog.
How can I select radioButton programatically?

Ragards
D.

Posted by Dude on September 26, 2007 at 09:54 PM PDT #

In the code in this example, you are using the row id to determine which row is selected. So, to set selected, you call setRBSelected(rowId). To select the radio button for the 5th row, you would call setRbSelected("4") (the rowId for the first row is "0"). But let's say that you need to look at the data to figure out which row should be selected. Say, for example, you want to pre-select the row for the person who has an id of 4. You would use code like this: String rowId = personDataProvider.findFirst("PERSON.PERSONID", 3).getRowId(); setRBSelected(rowId);

Note: You can also use the table phase listener for checkboxes and radio buttons in tables. See Winston's blog about this http://blogs.sun.com/winston/entry/multiple_selection_table1

Note that for radio buttons in Radio Button Groups, it is different. The following excerpt is from http://www.netbeans.org/kb/55/vwp-about_components.html, and is about all selection type components: "One way to preselect items is to call setSelected(Object[]) or setSelected(Object) from the prerender() method. You pass in the return values of the items that you want preselected. Be sure to verify that getSelected() returns null before setting the default options, or you will overwrite the user's selections on a post-back.

If the selection component is bound to a Default Options component, you can preselect items by choosing Configure Default Options from the selection component's pop-up menu. Select the Select Items checkbox, and select the default options that you want preselected.

Posted by Diva #2 on September 27, 2007 at 11:02 AM PDT #

Is it possible to have two radio buttons in a row and fire an event? Depending on which one is selected to control what choices are displayed in a dropdown list which is also part of the same row?

This should be by row and each row can have a different radio button selected.

And then capture the state of each row (what radio button selected and what choice in dropdown selected) and save that back to the database table?

I'm struggling with this one so thanks for any help you can provide.

Posted by Johnny Teunissen on September 28, 2007 at 08:49 AM PDT #

Many thanx Diva. you helped me.

Posted by Dude on September 28, 2007 at 07:33 PM PDT #

Hi.

Could you help me?
Your example don't work under NB6 :((
Maybe they some thing changed?

F.

Posted by Foli on January 17, 2008 at 10:30 PM PST #

Sorry all good works. I did mistake sorry.

Posted by Foli on January 18, 2008 at 12:10 AM PST #

Hi,
I'm working with the actual version Netbeans 6.1 and visualwebjsf. I'm trying your code but in fact the getSelected() method always returns 0.
Can I have a look at the complete States project to see what is missing or what went wrong. Can you help me?
Thanks you all , arragon

Posted by arragon on October 06, 2008 at 01:43 AM PDT #

Aaragon, I am sorry but I wrote that program more than 2 years ago and I no longer have a copy of it. If getSelected is returning 0, then I would think that setSelected is not being called. Are you sure that you you remembered to set the radio button's name property to buttonGroup? Did you do this step"Select Use Binding, Click the Bind to an Object tab, select RBSelected, and click OK."

Maybe you are having conversion or validation errors. Put a message group component on the page and put "info(set selected called)" in the setSelected method to verify it is getting called.

Note: I have not worked on Visual Web in a very long time. They may have made changes that broke this mini tutorial. If you are not able to get this to work, you can ask how to use radio buttons in tables in the netbeans user alias.

Posted by Christine Kutler on October 06, 2008 at 02:51 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