jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes keyevent not working in applet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "keyevent not working in applet?" Watch "keyevent not working in applet?" New topic
Author

keyevent not working in applet?

ajay smith
Greenhorn

Joined: Feb 02, 2013
Posts: 11
I've just started creating this portal game and I've encountered no problems except that KeyEvent isn't doing anything. Here's my code so far (in two classes):



and the second class:



Help please?
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
You need to add "keyaction" as KeyListener to the component that should listen to key events.
ajay smith
Greenhorn

Joined: Feb 02, 2013
Posts: 11
Where do I put that in/how do I do that? Kinda confused
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
Something like "addKeyListener(new keyaction())". You would do that in the init method. The thinkerbox method you should delete entirely. And the keyaction class needs to override one (or more) of the 3 methods it defines, not create a new method. And it must not create a new instance of the portal class.

Start by reading http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html to get some understanding how key listeners work.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 5048
    
  10

AWT was superseded by Swing more than 10 years ago. I recommend you change to Swing, which means extending JApplet instead of Applet and gaining a basic understanding of the component hierarchy imposed by the RootPaneContainer interface. Then you can leverage Swing's Key Bindings which afford much more flexibility than a simple KeyListener.

db


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: keyevent not working in applet?
 
It's not a secret anymore!