Skip to Main Content

Java SE (Java Platform, Standard Edition)

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.

JFormattedTextField - formatter returning Long ??

843806May 17 2008 — edited May 28 2008
I was using the default formatters for the following field and this worked fine.
{
      jOpNumTxtFld = new JFormattedTextField();                    
      jOpNumTxtFld.setValue( new Integer(0) );
      jOpNumTxtFld.addPropertyChangeListener("value", 
              new PropertyChangeListener()  {
                  public void propertyChange( PropertyChangeEvent evt ) {
                      int old = ((Integer)evt.getOldValue()).intValue();
                      int nwV = ((Integer)evt.getNewValue()).intValue();
                      if( old != nwV )  {  
                          opNum = nwV;
                      }
}
But I didn't like the group separator that the default formatters use so, on the suggestion of the
[How to Use Formatted Text Fields|http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html#factory] tutorial, I defined my own DefaultFormatterFactory:
{
          NumberFormat opnDisplayFormat = NumberFormat.getIntegerInstance();
          opnDisplayFormat.setGroupingUsed( false );
          NumberFormat opnEditFormat = NumberFormat.getIntegerInstance();
          opnEditFormat.setGroupingUsed( false );
          jOpNumTxtFld = new JFormattedTextField(
                    new DefaultFormatterFactory(
                        new NumberFormatter( opnDisplayFormat ),
                        new NumberFormatter( opnDisplayFormat ),
                        new NumberFormatter( opnEditFormat )));    
          jOpNumTxtFld.setValue( new Integer(0) );
          jOpNumTxtFld.addPropertyChangeListener("value", 
                    new PropertyChangeListener()  {
                        public void propertyChange( PropertyChangeEvent evt ) {
                            int old = ((Integer)evt.getOldValue()).intValue();
                            int nwV = ((Integer)evt.getNewValue()).intValue();
                            if( old != nwV )  {  
                                opNum = nwV;
                            }
}
Formats are now just how I like them. BUT now the control insists on translating an entered string into a Long. For instance, the first time the field loses focus I get a ClassCastException on +(Integer)evt.getNewValue()+. Only four digits are allowed in the field so a Long is pointless as well as being a pain in the butt to deal with.

What the heck is going on? And if there's a batter way to accomplish this please inform.

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 Jun 25 2008
Added on May 17 2008
9 comments
543 views