1 package edu.jiangxin.apktoolbox.swing.extend;
2
3 import javax.swing.text.*;
4
5
6
7
8
9
10
11
12 public class NumberPlainDocument extends PlainDocument {
13
14 private static final long serialVersionUID = 1L;
15
16 private int limit;
17
18 public NumberPlainDocument(int limit) {
19 super();
20 this.limit = limit;
21 }
22
23 @Override
24 public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
25 if (str == null) {
26 return;
27 }
28 if ((getLength() + str.length()) <= limit) {
29
30 char[] upper = str.toCharArray();
31 int length = 0;
32 for (int i = 0; i < upper.length; i++) {
33 if (upper[i] >= '0' && upper[i] <= '9') {
34 upper[length++] = upper[i];
35 }
36 }
37 super.insertString(offset, new String(upper, 0, length), attr);
38 }
39 }
40 }