jTextfield limitado

Pessoal, estou com problemas para limitar o tamanho de um jtextfield!!!
Alguem pode me ajudar??

[code]

package interfaces;

import javax.print.attribute.AttributeSet;
import javax.swing.text.PlainDocument;
import javax.swing.text.*;

class TamanhoMaximo extends PlainDocument
{
private int iMaxLength ;

  public TamanhoMaximo ( int maxlen ) 
  { 
        super () ; 
        iMaxLength = maxlen ; 
  } 



  public void insertString ( int offset , String str , AttributeSet attr ) 
        throws BadLocationException 
  { 
        if ( str == null ) 
        { 
              return ; 
        } 

        if ( iMaxLength <= 0 ) // aceitara qualquer no. de caracteres 
        { 
              super.insertString ( offset , str , (javax.swing.text.AttributeSet) attr ) ; 
              return ; 
        } 

        int ilen = ( getLength () + str.length () ) ; 
        if ( ilen <= iMaxLength ) // se o comprimento final for menor... 
        { 
              super.insertString ( offset , str , (javax.swing.text.AttributeSet) attr ) ; // ...aceita str 
        } 
  } 

} [/code]

Chamo o metodo desta meneira:

[code]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.*;
import javax.swing.text.Document;

import negocio.fachadaTipo;

public class GuiMoeda extends javax.swing.JFrame {

/** Creates new form GuiMoeda */

// Variables declaration - do not modify

private javax.swing.JTextField codigo;

// End of variables declaration

public GuiMoeda() {

  java.awt.GridBagConstraints gridBagConstraints; 

   
  codigo = new javax.swing.JTextField(); 
   
   
  getContentPane().setLayout(new java.awt.GridBagLayout()); 

  setTitle("Moedas"); 
   

  codigo.setDocument ( new TamanhoMaximo ( 2 ) ); 
  gridBagConstraints = new java.awt.GridBagConstraints(); 
  gridBagConstraints.gridx = 1; 
  gridBagConstraints.gridy = 1; 
  gridBagConstraints.ipadx = 50; 
  gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 
  getContentPane().add(codigo, gridBagConstraints); 

   

  setLocation(280, 180); 
  setResizable(false); 
  pack(); 

}

// </editor-fold>

/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GuiMoeda().setVisible(true);
}
});
}}[/code]

Eu faço assim:
Crio um documento:

package br.com.codesoftwares.swing.text;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class ExtremeDocument extends PlainDocument{
	
	private static final long serialVersionUID = 1L;
	
	public final static byte NORMAL		= 0;
	public final static byte LOWERCASE	= 1;
	public final static byte UPPERCASE	= 2;
	
	private byte caseRestriction        = NORMAL;
	private int maxLength               = -1;
	
	public ExtremeDocument() {
		super();
		setMaxLength(maxLength);
		setCaseRestriction(ExtremeDocument.NORMAL);
	}
	
	public int getMaxLength() {
		return maxLength;
	}
	
	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}
	
	public byte getCaseRestriction() {
		return caseRestriction;
	}
	
	public void setCaseRestriction(byte caseRestriction) {
		if (caseRestriction == NORMAL  || 
				caseRestriction == UPPERCASE ||
				caseRestriction == LOWERCASE) {
			this.caseRestriction = caseRestriction;
		} else {
			try {
				throw new Exception("Esta restri\u00E7\u00E3o " + caseRestriction + " n\u00E3o pode ser aplicada");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private String ajustCaseRestrictionString(String str) {
		if (getCaseRestriction() == UPPERCASE) {
			return str.toUpperCase();
		} else if (getCaseRestriction() == LOWERCASE) {
			return str.toLowerCase();
		}
		return str;
	}
	
	private int getTotalLength(String str) {
		return getLength()+str.length();
	}
	
	
	public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
		if (str==null) return;
		
		if (this.getMaxLength() < 0 || getTotalLength(str) <= this.maxLength) {
			super.insertString(offset, ajustCaseRestrictionString(str), attr);
		}
		else{
			if (getLength()==this.getMaxLength()) {
				// N�o faz nada o tamanho da string � igual ao permitido.
				return;
			}
			// Insere somente o tamanho permitido eliminando o que sobrou.
			String newStr = null;
			if(this.getMaxLength() < 0)
				newStr = str.substring(0, str.length());
			else
				newStr = str.substring(0, (this.maxLength-getLength()));
			super.insertString(offset, ajustCaseRestrictionString(newStr), attr);
		}
	}
}

e depois a classe textfield

package br.com.codesoftwares.swing.text;

import java.awt.Color;
import java.awt.event.FocusListener;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.Document;

import br.com.codesoftwares.swing.config.ComponentsExtreme;
import br.com.codesoftwares.swing.config.MnemonicController;
import br.com.codesoftwares.swing.config.PaintersExtreme;
import br.com.codesoftwares.swing.config.SwingConfig;

public class XTextField extends JTextField implements ComponentsExtreme, PaintersExtreme{
	
	private static final long serialVersionUID = 1L;
	
	public static Color mandatoryColor 	= SwingConfig.MANDATORY_COLOR;
	public static Color focusColor 		= SwingConfig.FOCUS_COLOR;
	public static boolean markMandatoryColor	= SwingConfig.MARK_MANDATORY_COLOR;
	public static boolean markFocusSelect 		= SwingConfig.MARK_FOCUS_SELECT;
	public static boolean markFocusColor 		= SwingConfig.MARK_FOCUS_COLOR;

	public XTextField() {
		super();
		initComponents();
	}

	public XTextField(String text) {
		super(text);
		initComponents();
	}

	public XTextField(int columns) {
		super(columns);
		initComponents();
	}

	public XTextField(String text, int columns) {
		super(text, columns);
		initComponents();
	}

	public XTextField(Document doc, String text, int columns) {
		super(doc, text, columns);
		initComponents();
	}

	private void initComponents() {
		setDocument(new ExtremeDocument());
		
		FocusListener focusListener = new FocusListener(){
			public void focusGained(java.awt.event.FocusEvent arg0) {
//				if(markFocusColor)
//					XTextField.this.setBackground(focusColor);
				if(markFocusSelect)
					XTextField.this.selectAll();
			};
			public void focusLost(java.awt.event.FocusEvent arg0) {
//				XTextField.this.setBackground(XTextField.this.isMandatory()?((markMandatoryColor)?mandatoryColor:SwingConfig.getTextFieldNormalColor(isEditable())):SwingConfig.getTextFieldNormalColor(isEditable()));
			};
		};
		
		addFocusListener(focusListener);
		
	}
	
	public JTextField getTextField(){
		return this;
	}
	
	public void setDocument(Document document){
		if(getDocument() == null){
			super.setDocument(document);
			return;
		}
		if(document != null){
			Object obj = getValueX(); 
			super.setDocument(document);
			setValueX(obj);
		}
	}
	
	public Document getDocument(){
		return super.getDocument();
	}
	
	public int getMaxLength() {
        if(getDocument() instanceof ExtremeDocument){
        	return ((ExtremeDocument)(getDocument())).getMaxLength();
        } else{
        	return -1;
        }
    }
    
    public void setMaxLength(int maxLength) {
    	if(getDocument() instanceof ExtremeDocument){
        	((ExtremeDocument)(getDocument())).setMaxLength(maxLength);
        	setValueX(getValueX());
        }
    }
    
    public byte getCaseRestriction() {
    	if(getDocument() instanceof ExtremeDocument){
    		return ((ExtremeDocument)(getDocument())).getCaseRestriction();
        } else{
        	return -1;
        }
    }
    
    public void setCaseRestriction(byte caseRestriction) {
    	if(getDocument() instanceof ExtremeDocument){
        	((ExtremeDocument)(getDocument())).setCaseRestriction(caseRestriction);
        	setValueX(getValueX());
        }
    }

    private boolean mandatory;
	public void setMandatory(boolean mandatory) {
		this.mandatory = mandatory;	
		if(markMandatoryColor)
			setBackground(isMandatory()?mandatoryColor:SwingConfig.getTextFieldNormalColor(isEditable()));
	}

	public boolean isMandatory() {
		return mandatory;
	}
	
	private String labelName;
	public void setLabelName(JLabel labelName, MnemonicController mnemonicController) {
		this.labelName = labelName.getText();
		if(mnemonicController != null){
			Character c = mnemonicController.createMnemonic(this.labelName);
			if(c != null){
				labelName.setDisplayedMnemonic(c);
				labelName.setLabelFor(this);
			}
		}
	}
	
	public void setLabelName(String labelName) {
		this.labelName = labelName;	
	}

	public String getLabelName() {
		return labelName;
	}

	public void setValueX(Object value) {
		if(value == null){
			setText(""); 
			return;
		}
		if(value instanceof String){
			setText((String)value);
			return;
		}
		setText(value.toString());
		return;
		
	}

	public Object getValueX() {
		return getText();
	}
	
	private boolean binding = true;
	public void setBinding(boolean binding) {
		this.binding = binding;		
	}
	
	public boolean isBinding() {
		return binding;
	}
	
	private boolean selectable = true;
	public boolean isSelectable() {
		return selectable;
	}

	public void setSelectable(boolean selectable) {
		this.selectable = selectable;
	}
	
	private String fieldName;
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;		
	}
	
	public String getFieldName() {
		if(fieldName == null || fieldName.equals("")) 
			return getLabelName();
		
		return fieldName;
	}
	
	private String extremeName = "Campo de Texto"; 
	public void setExtremeName(String extremeName) {
		this.extremeName = extremeName;
		
	}
	
	public String getExtremeName() {
		return extremeName;
	}

	public void doEmpty() {
		setText(""); 
		
	}
	
	public String verifyEmpty() {
		if(!isMandatory())
			return null;
		
		return (getText().equals(""))? 
				(	"Identificador" + ": " + getLabelName() + ".\n" +    
					"Tipo" + ": " + getExtremeName() + ".\n\n")   
					:
				(	null	);
	}
	
	public boolean isEnabledX() {
		return isEditable();
	}
	
	public void setEnabledX(boolean enabled) {
		setEditable(enabled);
		
	}

	private String status;
	public String getStatus() {
		if(status != null && !(status.equals(""))) 
			return getToolTipText() + "\n" + status; 
		
		return getToolTipText();
	}
	
	public void setStatus(String status) {
		this.status = status;		
	}
	
	public void reInit() {
		mandatoryColor 	= SwingConfig.MANDATORY_COLOR;
		focusColor 		= SwingConfig.FOCUS_COLOR;
		markMandatoryColor 		= SwingConfig.MARK_MANDATORY_COLOR;
		markFocusSelect 		= SwingConfig.MARK_FOCUS_SELECT;
		markFocusColor 		= SwingConfig.MARK_FOCUS_COLOR;
		
//		if(markMandatoryColor)
//			setBackground(isMandatory()?mandatoryColor:SwingConfig.getTextFieldNormalColor(isEditable()));
//		
//		if(isFocusOwner()){
//			if(markFocusColor)
//				setBackground(focusColor);
//			else
//				setBackground(SwingConfig.getTextFieldNormalColor(isEditable()));
//		}
				
		revalidate();
		repaint();
		
	}

}