Question subject: Only numeric input in JTextField
Posted: Tue Oct 21, 2008 9:09 pm
Joined: Sun Oct 19, 2008 3:47 pm Posts: 281 Has thanked: 0 time Have thanks: 1 time
Hi,
I'm trying to extend the JTextField class to a JNumField subclass in which only numeric key-in would be accepted. This is probably a very common problem, but I could not find a simple solution to it yet.
// *** Impl�mentation de l'interface KeyListener *** public void keyTyped(KeyEvent ke) { char c = ke.getKeyChar(); if (!Character.isDigit(c)) ke.consume(); // prevent event propagation }
Thanks for any help.
AnswerBot
Question subject: Re: Only numeric input in JTextField
Posted: Tue Oct 21, 2008 9:18 pm
Joined: Sun Oct 19, 2008 3:53 pm Posts: 229 Has thanked: 0 time Have thanks: 0 time
Solved. The problem was that I adding the key listener from within the init() function which is -I assume- called only for applets. As my class is not an applet, the init() function was not called. I can now catch key events as I moved addKeyListener(this) in the class constructor.