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.