package poryecto_practicas;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.PlainDocument;/** * * @author VaNcHoXcHk */public class sololetras extends PlainDocument{public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException { for (int i=0;i<arg1.length();i++) if (!Character.isLetter(arg1.charAt(i)) && !Character.isSpaceChar(arg1.charAt(i))) return; super.insertString(arg0, arg1, arg2); }}
package poryecto_practicas;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.PlainDocument;/** * * @author VaNcHoXcHk */public class solonumeros extends PlainDocument{public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException { for (int i=0;i<arg1.length();i++) if (!Character.isDigit(arg1.charAt(i))) return; super.insertString(arg0, arg1, arg2); }}
Muchas veces necesitamos que un campo solo reciba numeros o letras para proteger al usuario de posibles errores y ps tambien nos ahorramos nosotros como programadores algunos dolores de cabezaAqui el Proyect.y ps el code:Clase solo letrasCódigo: [Seleccionar]package poryecto_practicas;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.PlainDocument;/** * * @author VaNcHoXcHk */public class sololetras extends PlainDocument{public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException { for (int i=0;i<arg1.length();i++) if (!Character.isLetter(arg1.charAt(i)) && !Character.isSpaceChar(arg1.charAt(i))) return; super.insertString(arg0, arg1, arg2); }}clase solo numerosCódigo: [Seleccionar]package poryecto_practicas;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.PlainDocument;/** * * @author VaNcHoXcHk */public class solonumeros extends PlainDocument{public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException { for (int i=0;i<arg1.length();i++) if (!Character.isDigit(arg1.charAt(i))) return; super.insertString(arg0, arg1, arg2); }}Descarga del Proyecto http://www.4shared.com/file/Q2XVfSy4/Poryecto_practicas.html