hi, i'm getting an error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
it happens when i try to change the value inside a JFormattedTextField.
what i am trying to do is to see if the field requires an area code, and then if the user types in the matching area codes... if the field does NOT require an area code, the user may type something else., but i want to move the cursor to the appropriate position
/*
* Created on Aug 22, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package ca.sears.dsp.gui.widgets;
import java.text.ParseException;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.MaskFormatter;
import javax.swing.text.PlainDocument;
/**
* @author tchua2
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TelephoneField extends JFormattedTextField{
private static MaskFormatter mf2=null;
private String[] AreaCode = new String[1];
public boolean needsAreaCode = true;
private int numAreaCodes = 0;
public TelephoneField() throws ParseException{
super(new MaskFormatter("(###) ###-####"));
this.getDocument().addDocumentListener(new DocumentListener(){
public void insertUpdate(DocumentEvent arg0) {
if(needsAreaCode){
if(getText().length()==10){
transferFocus();
}
}else{
if(getPhoneNumber().length()<4){
System.out.println("less than 4");
boolean isCode = false;
for(int i=0;i<AreaCode.length;i++){
System.out.println("phone number "+getPhoneNumber());
System.out.println("area code "+AreaCode.substring(0,getPhoneNumber().length()));
if(getPhoneNumber().equals(AreaCode[i].substring(0,getPhoneNumber().length()))){
isCode = true;
}
}
System.out.println(isCode ? "true":"false");
if(isCode==false){
String number = getPhoneNumber();
int len = 3 - getPhoneNumber().length();
System.out.println("phone length"+getPhoneNumber().length());
System.out.println("lenght " + len);
if(len>0){
for(int k=0;k<len;k++){
number+=" ";
}
}
setValue("( ) "+number+"- ");
}
}
}
}
public void removeUpdate(DocumentEvent arg0) {}
public void changedUpdate(DocumentEvent arg0) {}
});
// this.addKeyListener(new KeyListener(){
// public void keyPressed(KeyEvent e){}
// public void keyTyped(KeyEvent e){
// System.out.println(getPhoneNumber());
// }
// public void keyReleased(KeyEvent e){
// if(getPhoneNumber().length()==10||getPhoneNumber().length()==7){
// if(isValidNumber()){
// transferFocus();
// }else{
// System.out.println("invalid");
// }
// }
// }
// });
}
private boolean isValidNumber(){
System.out.println("checking...");
boolean isValid = true;
if(needsAreaCode){
if(getPhoneNumber().length()!=10){
isValid=false;
System.out.println("length not 10");
}
}else{
if(getPhoneNumber().length()!=10&&getPhoneNumber().length()!=7){
isValid=false;
System.out.println("length not 7");
}else if(!(this.getText().substring(0,5).equals("( )"))){
isValid=false;
}
}
return isValid;
}
public String getPhoneNumber(){
String num = "";
for(int i=0;i<14;i++){
if(Character.isDigit(this.getText().charAt(i))){
num+=this.getText().substring(i,i+1);
}
}
return num;
}
public void addAreaCode(String code){
if(numAreaCodes==AreaCode.length){
String[] temp = new String[numAreaCodes+1];
System.arraycopy(AreaCode, 0, temp, 0, AreaCode.length);
AreaCode = temp;
}
AreaCode[numAreaCodes]=code;
numAreaCodes++;
}
}