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
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
427 views