01: // IntegerTextField.java
02: // $Id: IntegerTextField.java,v 1.4 2000/08/16 21:37:31 ylafon Exp $
03: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.jigadmin.widgets;
07:
08: import java.awt.event.KeyAdapter;
09: import java.awt.event.KeyEvent;
10:
11: import javax.swing.JTextField;
12:
13: /**
14: * A TextField that accepts only num char.
15: * @version $Revision: 1.4 $
16: * @author Benoît Mahé (bmahe@w3.org)
17: */
18: public class IntegerTextField extends JTextField {
19:
20: protected void processComponentKeyEvent(KeyEvent ke) {
21: if (ke.getKeyCode() == KeyEvent.VK_DELETE
22: || ke.getKeyCode() == KeyEvent.VK_BACK_SPACE
23: || ke.getKeyCode() == KeyEvent.VK_UP
24: || ke.getKeyCode() == KeyEvent.VK_DOWN
25: || ke.getKeyCode() == KeyEvent.VK_LEFT
26: || ke.getKeyCode() == KeyEvent.VK_RIGHT)
27: super .processComponentKeyEvent(ke);
28: char c = ke.getKeyChar();
29: if (!((c >= '0' && c <= '9') || (c == '-'))) {
30: ke.consume();
31: } else {
32: super .processComponentKeyEvent(ke);
33: }
34: }
35:
36: public IntegerTextField() {
37: super ();
38: }
39:
40: public IntegerTextField(int cols) {
41: super(cols);
42: }
43:
44: }
|