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:selectBooleanCheckbox and valueChangeListener problem

843844Sep 2 2009 — edited Sep 15 2009
Hello everyone!

I'm having trouble triggering method when user changes checkbox value.

In JSF file I have:
<h:selectBooleanCheckbox id="checkBoxPrijava"
styleClass="selectBooleanCheckbox"
value="#{prijava.selected}"
onchange="this.form.submit();"
immediate="true"
valueChangeListener="#{pc_XmlUpload.onCheckBoxPrijavaChanged}"  />
And in backing bean I have:
public void onCheckBoxPrijavaChanged(ValueChangeEvent event){
		System.out.println("Test" + event.getOldValue());
		facesContext.renderResponse();
	}
The problem is that onCheckBoxPrijavaChanged isn't triggered at all.

I've look at colleagues code and they are doing it same way.
Where can I start solving this problem?

Thanks for answers!

Ziga

Edited by: zigomir on Sep 2, 2009 6:11 AM

Comments

843844
It's possibly caused by one or both of the following causes: 1) For radio buttons and checkboxes you should use onclick instead of onchange to represent a change event. They're effectively only changed on 2nd click. 2) When the form is submitted, the valueChangeListner is ONLY invoked if the submitted input value is different from the initial input value. It is thus not always invoked.
843844
Finally working :) Thank you very much BalusC!
843844
You're welcome.
843844
Hello,
I have some questions too about how the h:selectBooleanCheckbox behaves with a valueChangeListener.
BalusC wrote:
It's possibly caused by one or both of the following causes: 1) For radio buttons and checkboxes you should use onclick instead of onchange to represent a change event. They're effectively only changed on 2nd click. 2) When the form is submitted, the valueChangeListner is ONLY invoked if the submitted input value is different from the initial input value. It is thus not always invoked.
What I'm trying to do is to add an item in a list whenever the user makes the h:selectBooleanCheckbox "checked" and remove an item from the list whenever the user unchecks the checkbox.

There are three issues that are causing me trouble at the moment.

First, the processValueChanged() function that servers the event is only called when the checkbox gets "checked" and not when it gets "unchecked", "effectively only changed on second click" as you mentioned above. The matter is, how can I capture the "uncheck" event ?

Secondly, the event.getNewValue() is always true when the processValueChanged() is called. You could say that this is normal since the event handler is called only in the case when we are going from "unchecked" --> "checked" aka, from false to true.

There are some cases though that are quite confusing.

For example if I have 2 checkboxes both checked, both with a registered valueChangeListener and both with an onclick="submit()" attribute, when I uncheck the first one, the form is submitted, but the processValueChanged() is called only for the second, still checked, checkbox and the event.getNewValue() is true!

In the above case, the behaviour that someone could assume is that the processValueChanged() is called for the first checkbox only (since this is the element whose value was changed) and that the event.getNewValue() is false.

Here are my binding and checks at the valueChangeListener function :
<h:selectBooleanCheckbox 
	rendered="#{cc.attrs.hasSelector}"
   	value="#{applicationInstance.contextParams['print'][cc.attrs.bindName]}"
   	onclick="this.form.submit();"
	valueChangeListener="${applicationInstance.processValueChange}"
	styleClass="#{cc.attrs.bindName}"
/>
I get more than one checkboxes by using ui:repeat ..

public void processValueChange(ValueChangeEvent event) throws AbortProcessingException 
{
	    if ( ((Boolean)event.getNewValue()) ) 
	    { ... }
}
Any comments/help are welcome :D

I did forget to mention that I'm using JSF 2.0 RC version...

Edited by: kpsichos on Sep 15, 2009 3:44 AM
843844
2 things you need to know:
The JSF valueChangeListener is ONLY called if the submitted value differs from the initial value.
The Javascript is just there to submit the form "automatically" without pressing the command button.

In the future, please start a new topic for each independent problem instead of resurrecting older topics and/or hijacking other's topics. If you found topics of relevance in your query, feel free to paste links in your new topic. Good luck.
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 13 2009
Added on Sep 2 2009
5 comments
8,437 views