|
|
From: Mathew
Category: Category 1
Date: 15 Oct 1998
Time: 15:05:21
Remote Name: 142.104.101.158
Hello, Can somebody help me with the following: I have a TextField, which is programmed to accept only letters (a..z and A..Z). Now I want to test whether it in fact does what it is supposed to. I did it manually, by first typing in an 'a' which was accepted and then I typed in a '1' and that was rejected. Then I tried to use java code to do what I did manually. That is, entering an 'a' or a '1' (using java code). Unfortunately, both of the characters are being accepted. After admitting defeat in trying to solve this problem, I am turning to the web. If anybody can tell me how this could be done, I would be much obliged. I am attaching the code that I wrote to do the above. I know that I could do it by overriding the keyPressed method, but I want to do it using the keyTyped method Mathew
import java.awt.*; import java.awt.event.*;
public class Test extends Frame { public Test() { setSize(100,100); t = new TextField(5); add(t); t.addKeyListener(new KeyTst()); show(); }
public static void main(String[] args) { new Test(); KeyEvent e = newKeyEvent(t,KeyEvent.KEY_TYPED, 20,0,KeyEvent.VK_UNDEFINED,'1'); Toolkit.getDefaultToolkit( ).getSystemEventQueue().postEvent(e); } static TextField t; }
class KeyTst extends KeyAdapter { public void keyTyped(KeyEvent evt) { if (!Character.isLetter(evt.getKeyChar())) evt.consume(); } evt.consume(); } }
Sadly, neither Web Developer's Journal, Markland Communities, Inc., Paige Turner nor anyone else associated with sponsoring these discussion groups can in any way be held responsible for any advice given here. The great unwashed masses of the public Internet hang out here and freely offer advice to all who seek it and some who don't. Take any advice received here with the proverbial grain of salt.Last changed: November 04, 1998 |
|
|
Discuss Subscribe Search |