Formatos en textfield de java

Empiezo a programar en java y tengo el siguiente problema:
Tengo un textfield donde quiero capturar datos por ejemplo un numero telefonico, pero quisiera que el textfield no aceptara mas de 7 digitos, que automaticamente ponga el guion separador en el numero telefonico, que no se introduzcan letras, etc. Es algo parecido a como se hace en visual foxpro, donde el cuadro de texto tiene una propiedad para darle formato a los datos.
Quisiera saber si en java existe lo mismo o hay alguna clase especial que pueda usar, o como puedo hacer.

1 Respuesta

Respuesta
1
Yo tenia un problema similar lo de los 7 digitos lo puedes solucionar con el siguiente codigo pero lo de la rayita una forma seria que programes cuando pierda el foco cuente los carectes y si es mayor a 6 tire la linea
package datos;
/**
* CIU_TextFieldFilter.java
*
*/
import javax.swing.text.*;
public class inv_TextFieldFilter extends PlainDocument
{
public static final String LOWERCASE = "abcdefghijklmnñopqrstuvwxyzáéíóúü";
public static final String UPPERCASE = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚÜ";
public static final String ALPHA = LOWERCASE + UPPERCASE + " ";
public static final String NUMERIC = "0123456789";
public static final String FLOAT = NUMERIC + ".";
public static final String ALPHA_NUMERIC = ALPHA + NUMERIC;
public static final String ALL = ALPHA + NUMERIC + " ¡!¿?.:,;()";
public static final String ALPHA_ESPC = ALPHA + " ";
public static final String EMAIL = ALPHA_NUMERIC + "@-_." ;
public static final String ALPHA_NUMERIC_ESPC = ALPHA_ESPC + NUMERIC;
public static final String DIRE = ALPHA_NUMERIC_ESPC + "#.,-_()";
public static final String TELF = NUMERIC + " /-()";
public static final String NOMB = ALPHA_ESPC + ".";
public static final String IVA = DIRE + "$%&/.:";
public static final String LOGIN = ALPHA_NUMERIC +"_";
private int limit;
protected String acceptedChars = null;
protected boolean negativeAccepted = false;
public inv_TextFieldFilter(String acceptedchars, int limit)
{
acceptedChars = acceptedchars;
this.limit = limit;
}
///////////////////
public inv_TextFieldFilter(int limit)
{
this(ALL, limit);
}
//////////////////////
public void setNegativeAccepted(boolean negativeaccepted)
{
if (acceptedChars.equals(NUMERIC) || acceptedChars.equals(FLOAT) || acceptedChars.equals(ALPHA_NUMERIC))
{
negativeAccepted = negativeaccepted;
acceptedChars += "-";
}
}
public void insertString(int offset, String str, AttributeSet attr)
throws BadLocationException
{
if (str == null) return;
if (acceptedChars.equals(UPPERCASE))
str = str.toUpperCase();
else if (acceptedChars.equals(LOWERCASE))
str = str.toLowerCase();
for (int i=0; i < str.length(); i++)
{
if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1)
return;
}
if (acceptedChars.equals(FLOAT) || (acceptedChars.equals(FLOAT + "-") && negativeAccepted))
{
if (str.indexOf(".") != -1)
{
if (getText(0, getLength()).indexOf(".") != -1)
{
return;
}
}
}
if (negativeAccepted && str.indexOf("-") != -1)
{
if (str.indexOf("-") != 0 || offset != 0 )
{
return;
}
}
if ((getLength() + str.length()) <= limit)
{
super.insertString(offset, str, attr);
}
}
}
con esta linea validas el JTEXTFields
txt_descripcion.setDocument(new inv_TextFieldFilter(inv_TextFieldFilter.ALPHA,20));
txt_idpais.setDocument(new inv_TextFieldFilter(inv_TextFieldFilter.NUMERIC,4));

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas