aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes addKeyListener() help needed plz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "addKeyListener() help needed plz" Watch "addKeyListener() help needed plz" New topic
Author

addKeyListener() help needed plz

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Hi, I'm using a class extending JApplet shown here, and also a java class that extends JPanel to do the drawing side of things.
I'm trying to get the KeyListener interface to work, but I don't know
how to add the addKeyListener in the constructor, becuase it works
differently to addMouseListener apparently.



And also a normal java class (PacmanGame) that extends JPanel:

I'm trying to insert the addKeyListener(...) method into the constructor
of PacmanGame, but I'm not sure how to do it. I think it has something to do with the contentPane, but I dont have a frame here, everything is being displayed in the HTML file that calls PacmanApplet.

Any help is much appreciated, thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Hi, thanks very much for your code.
But unfortunatley it doesnt seem to work
The applet shows the JLabel -- keys typed will appear here.
I used a println statement to in the keypressed method,
but still nothing.
I tried ammending my code first, and it didn't work, then
I copied down exactly what you wrote and compiled the two classes,
and executed the JApplet extended class, and still no response to
any key presses.
Did you miss anything from your code???
Thanks again for your efforts.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
works perfectly for me (copied the posted code into another file and ran it)

perhaps the cache (or whatever it is) is using an old version of PacmanGame.class
(this does happen)

create a new folder, create a new .java file, copy the posted code into the
new file, compile, then run the .html file.

the message "keys typed will appear here" will show, and if you press the x key
the message will be replaced by just the 'x'
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
It doesnt work, I've tried everything
What can I do???
I'm using netbeans 3.5, maybe my computer is dying
Thanks anyway, I'm gonna try putting the class files
on my website, maybe I can get that to fail too.
Ok, I've just tried that and as I said, it failed.
What am I doing wrong???

Thanks for your help anyway
[ November 09, 2005: Message edited by: colin shuker ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the only thing I can think of is perhaps something is stealing the focus
when the applet first opens (taking focus away from the JPanel)

try this change (when label shows, click into panel then press some keys)

I'm using 1.4.0_01 at the moment. If you're using any of the 1.5's,
the focus system is broken in many areas - this could be another

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
I just fell out of my chair in shock!!! (not really)
It worked!!! Well done mate, you are a pure genius.
I don't quite get what you've done, I need to use the mouse later,
as its pacman (for the buttons), but I usually just implement MouseListener and MouseMotionListener, I don't do what you've done here.

So... whats the difference between using listeners and using...
addMouseListener(new MouseAdapter(){ methods here }}); ???

Anyway, I'm glad you've solved this riddle, I felt like crying.
Thanks so much. I've never come across this requestFocusInWindow(); method before, so if you can explain, that would be great too.

Cheers
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
(generally) when you use an interface, you need to write the code for all
of the methods of the interface, even if they will be empty.

The ...Adapter classes already have empty methods written (for the
implemented interface), so, when using the ...Adapter class you are
overriding empty methods i.e. you only have to provide the code for the
method you want to use.


from the api docs:

requestFocusInWindow
public boolean requestFocusInWindow()Description copied from class:
Component
Requests that this Component get the input focus, if this Component's
top-level ancestor is already the focused Window. This component must be
displayable, visible, and focusable for the request to be granted. Every
effort will be made to honor the request; however, in some cases it may be
impossible to do so. Developers must never assume that this Component is the
focus owner until this Component receives a FOCUS_GAINED event.

This method returns a boolean value. If false is returned, the request is
guaranteed to fail. If true is returned, the request will succeed unless it
is vetoed, or an extraordinary event, such as disposal of the Component's
peer, occurs before the request can be granted by the native windowing
system. Again, while a return value of true indicates that the request is
likely to succeed, developers must never assume that this Component is the
focus owner until this Component receives a FOCUS_GAINED event.

This method cannot be used to set the focus owner to no Component at all.
Use KeyboardFocusManager.clearGlobalFocusOwner() instead.

The focus behavior of this method can be implemented uniformly across
platforms, and thus developers are strongly encouraged to use this method
over requestFocus when possible. Code which relies on requestFocus may
exhibit different focus behavior on different platforms.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: addKeyListener() help needed plz
 
Similar Threads
Problem with repaint()
keyListener problems
Add 2D shape to 2D shape and move the first one. Both should move.
Using KeyListener in an applet
Diff Between Applet and JApplet??