Boa Noite, utilizo a classe abaixo p/ limitar a digitação nos campos e transformar tudo em maiúsculo, gostaria de implementar tamanhoMinimoString, assim foçaria o usuário a digitar pelo menos um caracter no campo e não deixá-lo em branco… mas não estou conseguindo poderiam me ajudar? desde já agradeço.
1. import javax.swing.text.AttributeSet;
2. import javax.swing.text.BadLocationException;
3. import javax.swing.text.PlainDocument;
4.
5. /**
6. *
7. * @author Airton
8. *
9. */
10. public class MaiusculaLimitadaAsg extends PlainDocument {
11.
12. private int tamanhoMaximoString;
13.
14. // Booleano opcional caso queira tornar tudo maiúsculo
15. private boolean maiuscula = false;
16.
17. public MaiusculaLimitadaAsg(int tamanhoMaximoString) {
18. super();
19. this.tamanhoMaximoString = tamanhoMaximoString;
20. }
21.
22. public MaiusculaLimitadaAsg(int tamanhoMaximoString, boolean maiuscula) {
23. super();
24. this.tamanhoMaximoString = tamanhoMaximoString;
25. this.maiuscula = maiuscula;
26. }
27.
28. public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
29. if (str == null) return;
30.
31. if ((getLength() + str.length()) > tamanhoMaximoString){
32. str = str.substring(0,tamanhoMaximoString - getLength());
33. }
34. if ((getLength() + str.length()) <= tamanhoMaximoString) {
35. if (maiuscula){
36. str = str.toUpperCase();
37. }
38. super.insertString(offset, str, attr);
39. }
40. }
41. }