Ver Mensaje Individual
  #2 (permalink)  
Antiguo 02/05/2012, 14:08
anihag
 
Fecha de Ingreso: marzo-2012
Mensajes: 2
Antigüedad: 12 años, 1 mes
Puntos: 0
Respuesta: formato de un jTextFlield a numeros

Utiliza mejor un objeto PlainDocument

En el metodo insertString limita la longitud del JTextField haciendo algo así:

Código:
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
   {
	   if (this.getLength()+str.length()>3) return;
	   String allStr = this.getText(0, this.getLength());
       for (int i=0;i<str.length();i++){
    	   if (str.charAt(i)==',' && allStr.indexOf(',')>-1) return;
    	   if (!(Character.isDigit(str.charAt(i)) || str.charAt(i)==',' || str.charAt(i)=='.')) return;
       }
       super.insertString(offs, str, a);
   }
y lo asignas al JTextField con el metodo setDocument