Source code: com/eireneh/swing/NumericDocument.java
1
2 package com.eireneh.swing;
3
4 import javax.swing.*;
5 import javax.swing.text.*;
6
7 /**
8 * A numeric document simply extends document to refuse all non-numeric
9 * data entered according to Character.isDigit.
10 *
11 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
12 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
13 * Distribution Licence:<br />
14 * Project B is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General Public License,
16 * version 2 as published by the Free Software Foundation.<br />
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.<br />
21 * The License is available on the internet
22 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
23 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
25 * The copyright to this program is held by it's authors.
26 * </font></td></tr></table>
27 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
28 * @see docs.Licence
29 * @see java.lang.Character#isDigit(char)
30 * @author Joe Walker
31 */
32 public class NumericDocument extends PlainDocument
33 {
34 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
35 {
36 if (str == null) return;
37
38 char[] upper = str.toCharArray();
39 StringBuffer clear = new StringBuffer();
40
41 for (int i = 0; i<upper.length; i++)
42 {
43 if (Character.isDigit(upper[i])) clear.append(upper[i]);
44 }
45
46 super.insertString(offs, clear.toString(), a);
47 }
48 }