Meaningless Drivel is fun!*
The moose likes JSF and the fly likes Default selection of Radio Button when added to h:panelGrid Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Default selection of Radio Button when added to h:panelGrid" Watch "Default selection of Radio Button when added to h:panelGrid" New topic
Author

Default selection of Radio Button when added to h:panelGrid

Shemida Nivedha
Greenhorn

Joined: May 22, 2008
Posts: 18
Hi all,
I am new to JSF and I am trying out some examples.
My requirement is that I have to have a screen as below.

Label1 TextBox1

Label2 o Label3 TextBox2
o Label4 Menu1

where o represents a Radio Button.

I am able to get this structure but by default no radio buttons are selected. I wanted the first label namely the Label3 to be selected by default.
Kindly provide me solution as of what needs to be done to have the default selection in the radio buttons.
Thanks in advance,
Regards,
Shemida
Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 87
hi,

You can do it in the javascript when onload . for example
inside the onload function(){
var radioVal = document.getElementsByName('formname:selectoneradioname');

its returning array of radioVal. From this array you can select whatever you want

radioVal[0].checked=true;
or radioVal[1].checked=true;
Shemida Nivedha
Greenhorn

Joined: May 22, 2008
Posts: 18
Hi Ganesan,
Thanks for the info.
I tried the changes you have asked me to do. Javascript is also new to me ,so could you tell me if what I have done is right or not.
As of now the radio buttons are not getting selected by default.

My jsp is

<script type="text/javascript">
function initForm() {
var radioVal = document.getElementById("addVmsHostForm:radio1");
radioVal[0].checked=true;
}
</script>
</head>

I call the initForm() in the onload of <body>.

the html regenerated code for radio button is

<tr>
<td><table border="1" id="jbpns_2fprov_2fdefault_2fotherAjaxportletWindowsnpbj:j_id_jsp_1559037212_0:AddVMSHost:addVmsHostForm:radio1" class="selectOneRadio">
<tr>
<td>
<input type="radio" name="jbpns_2fprov_2fdefault_2fotherAjaxportletWindowsnpbj:j_id_jsp_1559037212_0:AddVMSHost:addVmsHostForm:radio1" id="jbpns_2fprov_2fdefault_2fotherAjaxportletWindowsnpbj:j_id_jsp_1559037212_0:AddVMSHost:addVmsHostForm:radio1:0" value="1" /><label for="jbpns_2fprov_2fdefault_2fotherAjaxportletWindowsnpbj:j_id_jsp_1559037212_0:AddVMSHost:addVmsHostForm:radio1:0"> HostURL </label></td>
</tr>
<tr>
<td>
<input type="radio" name="jbpns_2fprov_2fdefault_2fotherAjaxportletWindowsnpbj:j_id_jsp_1559037212_0:AddVMSHost:addVmsHostForm:radio1" id="jbpns_2fprov_2fdefault_2fotherAjaxportletWindowsnpbj:j_id_jsp_1559037212_0:AddVMSHost:addVmsHostForm:radio1:1" value="2" /><label for="jbpns_2fprov_2fdefault_2fotherAjaxportletWindowsnpbj:j_id_jsp_1559037212_0:AddVMSHost:addVmsHostForm:radio1:1"> Address Name</label></td>
</tr>
</table></td>
</tr>

Can you tell me where I went wrong?
Thanks for your help.
Regards,
Shemida
Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 87
Hi,

Can you show your radio button part of code in your jsp(not the rendered html code).
[ June 03, 2008: Message edited by: Ganesan Ramakrishnan ]
Shemida Nivedha
Greenhorn

Joined: May 22, 2008
Posts: 18
Hi,
I am attaching the code for your reference.

<h:panelGrid columns="2">
<hutputText styleClass="portal-font" value="Host Name" />
<h:inputText required="true" />

<hutputText styleClass="portal-font" value="Host Address" />

<h:panelGroup>
<h:panelGrid columns="2" border="1">
<h:panelGrid columns="1">
<h:selectOneRadio disabledClass="selectOneRadio_Disabled"
styleClass="selectOneRadio" id="radio1" layout="pageDirection"
rendered="true" border="1">
<f:selectItem itemValue="1" itemLabel="HostURL " />
<f:selectItem itemValue="2" itemLabel="Address Name" />
</h:selectOneRadio>
</h:panelGrid>
<h:panelGrid columns="1" border="1">
<h:inputText required="true" />
<h:selectOneMenu id="theme"
valueChangeListener="#{VoiceMailServerDetails.valueChanged}">
<f:selectItems value="#{VoiceMailServerDetails.hostAddressMenu}" />
</h:selectOneMenu>
</h:panelGrid>
</h:panelGrid>
</h:panelGroup>

Regards,
Shemida
Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 87
Hi Shemida,

I tried in my system, its working fine. Just give the id to <f:selectItem> and also give the proper id of <h:selectOneRadio to javascript code snippet.

Regards
Ganesan
Shemida Nivedha
Greenhorn

Joined: May 22, 2008
Posts: 18
Hi,
Could you tell as in what id needs to be given in the javascript?
As of now I have the id for selectRadioButton to be "radio1".
DO I need to give the id for each selectItem also?
If so by which id can i refer to the radioButtons from the Javascript.
Sorry I tried out many possibilities & i could not get this done.
Kindly give me the exact details as of what I must add in the javascript.
Regards,
Shemida
Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 87
<h:selectOneRadio > id is the right one , i think you too have the same one inside the javascript.
Yes you need to give the different id for each <f:selectItem > too. this is not for inside the javascript. You should give the id
for all components , its recommended one. If the result is not coming means ,You just remove the panelgroup and panelgrid tag, then try that

Regards
Ganesan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default selection of Radio Button when added to h:panelGrid
 
Similar Threads
Struts 1.1: html:radio issues
two really important questions
model for group of radio buttons
How can I disable a text input field?
Problem in Struts radio button