• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help to map KeyStrokes in ActionMap using CardLayout in a JApplet

 
Ruth Bugembe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys Thank you for your help. I am developing a game where a player has to find a path in a maze. Right now the player can only click the buttons on the maze. I need an option to use the arrow keys. Iam having problems.

I have picked up pieces from my code to illustrate my problem. I am very grateful for your help and time. Below is the code. If I call "AnyClass" in the "AppletClass" it works but when I call "MiddleClass" in the "AppletClass" and MiddleClass" calls "AnyClass" it does not work. In my project at www.hiredforoneday.com I call 4 panels in CardLayout before I call "AnyClass" which is the "Maze"





Thank you for your time

Ruth
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too much code to look at in detail. A couple of comments:

1. I have never used the setInputMap() or setActionMap() method and I don't see any need for you to use them either.

2. If you code works in one case but not the other then you are probably accessing the wrong InputMap/Action map. You can try:

InputMap im = compnent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

or maybe you are adding the KeyBindings to the wrong component. Maybe you can just try to add the key bindings to the root pane of the applet instead of all the individual panels.
 
Ruth Bugembe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody Thank you very much for the time you have take to look at my post.
Please I need your help.

I have a project where I need the player to have the option to use arrow keys to play or clicking the buttons.

There is something that goes wrong with my code whether I use Key Mapping or KeyListener.

The Code I have submitted uses the KeyListener.

If CardLayoutClass calls the GraphicsClass from the AppletClass the arrow keys work,

if CardLayoutClass calls MiddleClass from the AppletClass and then calls GraphicsClass from the MiddleClass the Arrow keys do not work.

In my project CardLayoutClass calls 4 panels before the GraphicsClass “Maze” is called.
I suspect I need the button in the MiddleClass to respond to the KeyListener in the GraphisClass.

Please do not use the “this” reference I run in a lot problems because I am doing a lot of class reference in my project.

Please look at www.hiredforoneday.com to understand my problem

Thank you again for taking the time to look at my code
Ruth

Below is the code The ball will only move right to shorten the code. Please uncomment in the AppletClass the call to GraphicsClass and comment out the call to MiddleClass and it will work










 
Ruth Bugembe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody. Thank you for the time you took to look at my code.

I have found out that my problem had nothing to do KeyListener or Key Mapping. it was something to do with CardLayout or "Focus_Cycle inside one Container" I am no expert. below is he code that solved my problem.



Thanks for all

Ruth
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should NOT be using requestFocus(). Read the API description and it will tell you the appropriate method to use.

If a focus request fixed your problem then the problem is because you are using a KeyListener.

a) A KeyListener only works when a component has focus.
b) When using Key Bindings you can set the binding to work even when the component doesn't have focus. That is why we always suggest using Key Bindings.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!