Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

<h:selectOneRadio> problem

843844Jun 1 2009 — edited Jun 3 2009
Hi every body ,
I have a serious problem in jsf <h:selectOneRadio> component.

I am using two different <h:selectOneRadio> component .When I am clicking the first radio button I can get data from the data base & showing in my jsf , same as in the second radio button .
My problem is , I want to un check the first radio when I am clicking the second one .
Here is my code .
<h:selectOneRadio layout="pageDirection"  immediate ="true"  									valueChangeListener="#{multiSite.getValue1}" onclick="submit()">
	<f:selectItem id="item1" itemLabel="My value-1" itemValue="1" />
</h:selectOneRadio>
<h:outputText value="" />

<h:panelGrid width="100%" rendered="#{multiSite.renderValue1}">
	<h:selectOneRadio styleClass="radioButtonDataText" layout="pageDirection"  immediate ="true" >
		<f:selectItems value="#{multiSite.myMap1}"/>
	</h:selectOneRadio>
	<h:outputText value="" />
	<h:outputText value="" />
</h:panelGrid>

<h:selectOneRadio layout="pageDirection" immediate ="true" 
valueChangeListener="#{multiSite.getValue2}" onclick="submit()" >
	<f:selectItem id="item2" itemLabel="My value-2" itemValue="2" />
</h:selectOneRadio>

<h:panelGrid width="100%" rendered="#{multiSite.renderValue2}">
	<h:selectOneRadio styleClass="radioButtonDataText" layout="pageDirection"  immediate ="true" >
		<f:selectItems value="#{multiSite.myMap2}"/>
	</h:selectOneRadio>
	<h:outputText value="" />
	<h:outputText value="" />
</h:panelGrid>
Thanks ,
SB

Comments

843844
If you want to do this at the server side, just set its value property to null.
If you want to do this at the client side, just use Javascript for this.
843844
Thanks once again .
I can do it by the java script , but I want to do it on the server side.
Could you please give some code example by which I can do it on the server side ?

Regards,
SB
843844
Set its value property to null.
selectOneRadioValue = null;
843844
Thanks again ,
Could you please give some example , am not getting this .

regards,
SB
843844
How are you getting the selected value in the backing bean then?
843844
Thanks for your reply again ,

Here is my jsf code :
   <h:selectOneRadio id="radio1" layout="pageDirection" styleClass="boldText" 
	immediate ="true" valueChangeListener="#{multiSite.getValue1}" >
	<f:selectItem id="item1" itemLabel="Value1" itemValue="1" />
	<a4j:support event="onchange" reRender="form" focus="radio1"/>
    </h:selectOneRadio>

<h:selectOneRadio id="radio2" layout="pageDirection" immediate ="true" styleClass="boldText"
	valueChangeListener="#{multiSite.getValue2}" >
	<f:selectItem id="item2" itemLabel="Value2" itemValue="2" />
	<a4j:support event="onchange" reRender="form" focus="radio2"/>
</h:selectOneRadio>
Here is my backing bean
       //For the first radio button event :
   	public void getValue1(ValueChangeEvent valueChangeEvent) throws Exception
	{
		if(valueChangeEvent.getNewValue()!=null)
		{	
			System.out.println("Getting the first radio button's value   "+(String)valueChangeEvent.getNewValue()).toString();
			
		}
		
	}

        //For the second radio button event :
   	public void getValue2(ValueChangeEvent valueChangeEvent) throws Exception
	{
		if(valueChangeEvent.getNewValue()!=null)
		{	
			System.out.println("Getting the second radio button's value   "+(String)valueChangeEvent.getNewValue()).toString();
			
		}
		
	}
These are my codes .
Please give your valuable opinion , about this .

regards,
SB
843844
Ah yes, you're misusing the valueChangeListener.

Well, dude, you should basically set its value to null to "remove" the selected item. The selected item is set as the value of h:selectOneRadio. The h:selectOneRadio has a 'value' attribute. Bind it to a bean property. That's the selected item. In the bean you can simply get/set/null this value.
843844
Ya ,
I have already done that , but the value property is not giving the proper output & when I was giving it the null value , it was still showing checked .

please go through this code :

jsf :
<h:selectOneRadio id="radio1"  value="#{multiSite.radio1Val}"  layout="pageDirection" styleClass="boldText" 
	immediate ="true" valueChangeListener="#{multiSite.getValue1}" >
	<f:selectItem id="item1" itemLabel="Value1" itemValue="1" />
	<a4j:support event="onchange" reRender="form" focus="radio1"/>
    </h:selectOneRadio>
 
<h:selectOneRadio id="radio2" layout="pageDirection" value="#{multiSite.radio2Val}"  immediate ="true" styleClass="boldText"
	valueChangeListener="#{multiSite.getValue2}" >
	<f:selectItem id="item2" itemLabel="Value2" itemValue="2" />
	<a4j:support event="onchange" reRender="form" focus="radio2"/>
</h:selectOneRadio>
Here is the backing bean code :

when am clicking the first radio button , I just setting the value property of second to null &
same for the second radio button.
//private variables :

private String radio1Val;
private String radio2Val;


//For the first radio button event :
   	public void getValue1(ValueChangeEvent valueChangeEvent) throws Exception
	{
		if(valueChangeEvent.getNewValue()!=null)
		{	
			System.out.println("Getting the first radio button's value   "+(String)valueChangeEvent.getNewValue()).toString();
                         
                        //setting the value of second radio to null.
                        if(getRadio2Val!=null)
                        {
                             this.setRadio2Val(null);
                        }
			
		}
		
	}
 
        //For the second radio button event :
   	public void getValue2(ValueChangeEvent valueChangeEvent) throws Exception
	{
		if(valueChangeEvent.getNewValue()!=null)
		{	
			System.out.println("Getting the second radio button's value   "+(String)valueChangeEvent.getNewValue()).toString();

                         //setting the value of first radio to null.
                        if(getRadio1Val!=null)
                        {
                             this.setRadio1Val(null);
                        }
			
		}
		
	}

//setter & getter methods 

	/**
	 * @return the radio1Val
	 */
	public String getRadio1Val() {
		return radio1Val;
	}

	/**
	 * @param radio1Val the radio1Val to set
	 */
	public void setRadio1Val(String radio1Val) {
		this.radio1Val = radio1Val;
	}

	/**
	 * @return the radio2Val
	 */
	public String getRadio2Val() {
		return radio2Val;
	}

	/**
	 * @param radio2Val the radio2Val to set
	 */
	public void setRadio2Val(String radio2Val) {
		this.radio2Val = radio2Val;
	}
regards,
SB
843844
Misusing the valueChangeListener as action method does indeed not help. It runs during 3rd phase of JSF lifecycle, while bean properties are set during 4th phase and the action (commandbutton/link) is normally executed during 5th phase. Learn the JSF lifecycle properly. This article may help: [http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html].

So, you need to queue the valueChangeEvent to 5th phase using ValueChangeEvent#queue(), or to skip the subsequent phases using FacesContext#responseComponete().

By the way, as you're using Ajax4jsf, why don't you just use it instead to change/populate the dropdowns?
843844
Very much thanks for this reply , I am new in jsf & today I have gained lot of information by you dude .

Could you please explain how could I use valueChangeEvent.queue() or FacesContext#responseComplete() to solve
this problem .

regards,
SB
843844
sb.majumder_07 wrote:
Could you please explain how could I use valueChangeEvent.queue() or FacesContext#responseComplete() to solve
this problem .
[http://google.com/search?q=ValueChangeEvent%23queue()+site:forums.sun.com]
[http://google.com/search?q=FacesContext%23responseComplete()+site:forums.sun.com]
843844
Thanks for your reply .

Here I have implemented the valueChangeEvent.queue(), but it is not working properly.

Here is my jsf code :
<h:selectOneRadio id="radio1"  value="#{multiSite.radio1Val}"  layout="pageDirection" styleClass="boldText" 
 	immediate ="true" valueChangeListener="#{multiSite.getValue1}" >
	<f:selectItem id="item1" itemLabel="Value1" itemValue="1" />
	<a4j:support event="onchange" reRender="form" focus="radio1"/>
    </h:selectOneRadio>
 
<h:selectOneRadio id="radio2" layout="pageDirection" value="#{multiSite.radio2Val}"  immediate ="true" styleClass="boldText"
	valueChangeListener="#{multiSite.getValue2}" >
	<f:selectItem id="item2" itemLabel="Value2" itemValue="2" />
	<a4j:support event="onchange" reRender="form" focus="radio2"/>
</h:selectOneRadio>
Here is my backing bean code :
private String radio1Val;
private String radio2Val;
 
 
         //For the first radio button event :
   	public void getValue1(ValueChangeEvent valueChangeEvent) throws Exception
	{
		if (valueChangeEvent.getPhaseId() == PhaseId.INVOKE_APPLICATION) 
                {
                        
                        System.out.println(this.getRadio2Val());
                        if(getRadio2Val()!=null)
                        {
                             this.setRadio2Val(null);
                        }
			
		   
                } else 
                 {
                       valueChangeEvent.setPhaseId(PhaseId.INVOKE_APPLICATION);
                       valueChangeEvent.queue();
                 }
	}
 
        //For the second radio button event :
   	public void getValue2(ValueChangeEvent valueChangeEvent) throws Exception
	{
                    if (event.getPhaseId() == PhaseId.INVOKE_APPLICATION) 
                    {
                             //setting the value of first radio to null.
                            if(getRadio1Val!=null)
                            {
                                this.setRadio1Val(null);
                            }
                   } else 
                    {
                            event.setPhaseId(PhaseId.INVOKE_APPLICATION);
                           event.queue();
                    }
	}
 
//setter & getter methods 
 
	/**
	 * @return the radio1Val
	 */
	public String getRadio1Val() {
		return radio1Val;
	}
 
	/**
	 * @param radio1Val the radio1Val to set
	 */
	public void setRadio1Val(String radio1Val) {
		this.radio1Val = radio1Val;
	}
 
	/**
	 * @return the radio2Val
	 */
	public String getRadio2Val() {
		return radio2Val;
	}
 
	/**
	 * @param radio2Val the radio2Val to set
	 */
	public void setRadio2Val(String radio2Val) {
		this.radio2Val = radio2Val;
	}
When I am clicking the first radio button it is going to the server & executing the event & became un check the first radio button .
Same as for the second radio also.
One more thing when I am clicking the first radio button the value is not binding to the backing bean property .
Please help.

regards ,
SB
843844
Thanks dude ,
I overcame the problem :)
1 - 13
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 1 2009
Added on Jun 1 2009
13 comments
893 views