- import javax.swing.text.*;
- private int limit;
- // optional uppercase conversion
- private boolean toUppercase = false;
- JTextFieldLimit(int limit) {
- super();
- this.limit = limit;
- JTextFieldLimit(int limit, boolean upper) {
- super();
- this.limit = limit;
- toUppercase = upper;
- }
- @Override
- public void insertString
- if (str == null) return;
- if ((getLength() + str.length()) <= limit) {
- if (toUppercase) str = str.toUpperCase();
- super.insertString(offset, str, attr);
- }
- }
- }
Y la forma de usarlo es la siguiente
- JTextField1.setDocument(new JTextFieldLimit(12));
Et voilà, estás listo =)
Saludos a Tod@s, y espero que les ayude!
PS:Este código no fue hecho por mí, sólo fue encontrado Googleando ;)
No hay comentarios:
Publicar un comentario