Problemas con los JtextField
Hola, tengo un problema con los JtextField. Lo que sucede es que estoy haciendo un programa de circuitos digitales, y quiero poner en un JtextField las entradas, y mi prolema es que COMO PUEDO HACER PARA QUE SOLO SE PUEDA ESCRIBIR O 0 O 1, y no cualquier dato que no sean valores binarios.
Muchas gracias, un saludo
Muchas gracias, un saludo
Necesitas un Filtro que controles los caracteres introducidos:
public class FiltroDocumento extends PlainDocument {
//conjunto de caracteres a elegir como filtro
public static final String LOWERCASE
= " abcdefghijklmnñopqrstuvwxyz1234567890áéíóú!¡"$%&/()=?¿'|@#~><{}[]*/+-.:,;._ªºn";
public static final String UPPERCASE
= " ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890ÁÉÍÓÚ¡!"$%&/()=?¿'|@#~><{}[]*/-+.:,;._ªºn";
public static final String BINARY = "01";
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;
protected String acceptedChars = null;
protected boolean negativeAccepted = false;
public FiltroDocumento()
{
this(ALPHA_NUMERIC);
}
public FiltroDocumento(String acceptedchars)
{
this.acceptedChars = acceptedchars;
}
/**
* establece el signo "-" como caracter valido
*/
public void setNegativeAccepted(boolean negativeaccepted)
{
if (acceptedChars.equals(NUMERIC) ||
acceptedChars.equals(FLOAT) ||
acceptedChars.equals(ALPHA_NUMERIC)){
negativeAccepted = negativeaccepted;
acceptedChars += "-";
}
}
/**
* Sobreescritura del método: inserta los caracteres que se escriben
* filtrados según el tipo de tipografía que se elige
*
* @param offset
* @param str
* @param attr
* @throws BadLocationException
*/
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;
}
}
super.insertString(offset, str, attr);
}
}
Esto se aplica a un JTextField del siguiente modo:
JTextField t = new JTextField();
FiltroDocumento filtro = new FiltroDocumento(FiltroDocumento.BINARY);//filtro a elegir
t.setDocument(filtro);
public class FiltroDocumento extends PlainDocument {
//conjunto de caracteres a elegir como filtro
public static final String LOWERCASE
= " abcdefghijklmnñopqrstuvwxyz1234567890áéíóú!¡"$%&/()=?¿'|@#~><{}[]*/+-.:,;._ªºn";
public static final String UPPERCASE
= " ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890ÁÉÍÓÚ¡!"$%&/()=?¿'|@#~><{}[]*/-+.:,;._ªºn";
public static final String BINARY = "01";
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;
protected String acceptedChars = null;
protected boolean negativeAccepted = false;
public FiltroDocumento()
{
this(ALPHA_NUMERIC);
}
public FiltroDocumento(String acceptedchars)
{
this.acceptedChars = acceptedchars;
}
/**
* establece el signo "-" como caracter valido
*/
public void setNegativeAccepted(boolean negativeaccepted)
{
if (acceptedChars.equals(NUMERIC) ||
acceptedChars.equals(FLOAT) ||
acceptedChars.equals(ALPHA_NUMERIC)){
negativeAccepted = negativeaccepted;
acceptedChars += "-";
}
}
/**
* Sobreescritura del método: inserta los caracteres que se escriben
* filtrados según el tipo de tipografía que se elige
*
* @param offset
* @param str
* @param attr
* @throws BadLocationException
*/
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;
}
}
super.insertString(offset, str, attr);
}
}
Esto se aplica a un JTextField del siguiente modo:
JTextField t = new JTextField();
FiltroDocumento filtro = new FiltroDocumento(FiltroDocumento.BINARY);//filtro a elegir
t.setDocument(filtro);