• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

addKeyListener() help needed plz

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic