Como el contenido de un textField ???
Hola quiero validar el contenido del textField para que solo me acepte entero,,, alguien sabe el metodo ???
checquea este website: http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html
esta es una clase que valida lo que quieres, solamente tienes que hacer una instancia de la clase se la siguiente manera;
myTextField.setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
espero y te sirva de algo
Felices lineas!
package sphinx.fac.comunes;
import javax.swing.text.*;
import java.awt.event.KeyEvent;
public class JTextFieldFilter extends PlainDocument {
public static final String LOWERCASE =
"abcdefghijklmnopqrstuvwxyz";
public static final String UPPERCASE =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
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 JTextFieldFilter() {
this(ALPHA_NUMERIC);
}
public JTextFieldFilter(String acceptedchars) {
acceptedChars = acceptedchars;
acceptedChars+=(char)KeyEvent.VK_SPACE;
acceptedChars+=(char)KeyEvent.VK_COMMA;
acceptedChars+=String.valueOf("#");
acceptedChars+=String.valueOf("@");
acceptedChars+=String.valueOf(".");
}
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;
}
}
super.insertString(offset, str, attr);
}
}
myTextField.setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
espero y te sirva de algo
Felices lineas!
package sphinx.fac.comunes;
import javax.swing.text.*;
import java.awt.event.KeyEvent;
public class JTextFieldFilter extends PlainDocument {
public static final String LOWERCASE =
"abcdefghijklmnopqrstuvwxyz";
public static final String UPPERCASE =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
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 JTextFieldFilter() {
this(ALPHA_NUMERIC);
}
public JTextFieldFilter(String acceptedchars) {
acceptedChars = acceptedchars;
acceptedChars+=(char)KeyEvent.VK_SPACE;
acceptedChars+=(char)KeyEvent.VK_COMMA;
acceptedChars+=String.valueOf("#");
acceptedChars+=String.valueOf("@");
acceptedChars+=String.valueOf(".");
}
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;
}
}
super.insertString(offset, str, attr);
}
}