SoNumeros.java
975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gerador;
/**
*
* @author felipel
*/
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class SoNumeros extends PlainDocument {
private int maxlength;
public SoNumeros() {
}
public SoNumeros(int maxlength) {
super();
this.maxlength = maxlength;
}
public void insertString(int offs, String str, AttributeSet a) {
try {
Integer.parseInt(str);
} catch (NumberFormatException ex) {
str = "";
}
try {
boolean fixedLengh = (!((getLength() + str.length()) > maxlength));
if (maxlength == 0 || fixedLengh) {
super.insertString(offs, str, a);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}