Click to See Complete Forum and Search --> : [SOLVED] Applet & keypressedEvent


ask_123
01-31-2005, 12:03 PM
I am coding an applet application which contains some basic AWT componets, such as TextField, TextArea.

Due to the specific application, users can only use the KB to interact with the GUI. For example, press 'P' to set focus to one of the text field and input information, press enter to connect to a database ..etc.

My applet implement the KeyListener and the code is like this

TextField tf = new TextField(20);
public void init() {
...

this.add(tf);
addKeyListener(this);
...
}

public void ketTyped(KeyEvent e) {
tf.requestFocus();
tf.setText("The text.");
}

However, these do not work, the applet is not responding to any kettyped. Anybody can give me any suggestion?

Thanks in advance

ALex

bwkaz
01-31-2005, 08:07 PM
Originally posted by ask_123
addKeyListener(this); It's been forever since I did Java, but shouldn't this be:

tf.addKeyListener(this);

?

ask_123
01-31-2005, 09:26 PM
Thanks bwakz, this is where the problem is.

I need to trap a key press before the component get focus. For example, when the user press 'P', one particular component should get the focus and allow input of data in that component.

Therefore I add the keyListener to the applet itself instead of a particular component.

ask_123
02-02-2005, 02:33 AM
Bwkaz

It looks like you are always right. I have to register a keylistener to every components in the applet and use the same code to handle the key events.

I cannot just register the applet itself to listen for its own key event. I can only do that if if the applet itself is a graphic area.

Thanks for your help


Alex

bwkaz
02-02-2005, 07:53 PM
Heh -- not always. ;)

But glad you got it working!