add keyy listener to jpanel

hello, i have a class that extends jpanel and try to set a key listener on this panel but nothing happens when i press the key here is the code if someone can help :

publicclass BackgroundPanelextends JPanelimplements KeyListener

{

/** Creates a new instance of BackgroundPanel */

public BackgroundPanel()

{

addKeyListener(this);

}

publicvoid keyTyped(KeyEvent e)

{

}

publicvoid keyPressed(KeyEvent e)

{

int c = e.getKeyCode();

switch(c)

{

case KeyEvent.VK_LEFT :

System.out.println("left key pressed");

break;

case KeyEvent.VK_RIGHT :

System.out.println("right kkey pressed");

break;

}

}

publicvoid keyReleased(KeyEvent e)

{

int c = e.getKeyCode();

switch(c)

{

case KeyEvent.VK_LEFT :

System.out.println("left key released");

break;

case KeyEvent.VK_RIGHT :

System.out.println("right key release");

break;

}

}

[2300 byte] By [JusteUneQuestiona] at [2007-11-27 6:15:40]
# 1
I'm not a Swing specialist, but I'm not sure JPanels accept keystrokes, and they may not be able to interact with keylisteners. you may need some other component that can accept keystrokes.Of course I could be spewing a lot of hooey.
petes1234a at 2007-7-12 17:26:32 > top of Java-index,Desktop,Core GUI APIs...
# 2
ok that is what i thought so i guess the only container that can accept keystrokes are JFrame.
JusteUneQuestiona at 2007-7-12 17:26:32 > top of Java-index,Desktop,Core GUI APIs...
# 3
Sorry, I was wrong. Look [url= http://forum.java.sun.com/thread.jspa?threadID=560708&tstart=135]here[/url]
petes1234a at 2007-7-12 17:26:32 > top of Java-index,Desktop,Core GUI APIs...
# 4

thanks for this useful link, unfortunately its still not working, i probably did something wrong, here is the code in my jpanel's constructor :

Action actionRightArrowPressed = new AbstractAction()

{

public void actionPerformed(ActionEvent e)

{

((Flip)lesObstacles.get(6)).bouge();

System.out.println("right kkey pressed");

repaint();

}

};

Action actionLeftArrowPressed = new AbstractAction()

{

public void actionPerformed(ActionEvent e)

{

((Flip)lesObstacles.get(5)).bouge();

System.out.println("left key pressed");

repaint();

}

};

Action actionRightArrowRelease = new AbstractAction()

{

public void actionPerformed(ActionEvent e)

{

((Flip)lesObstacles.get(6)).release();

System.out.println("right key release");

repaint();

}

};

Action actionLeftArrowRelease = new AbstractAction()

{

public void actionPerformed(ActionEvent e)

{

((Flip)lesObstacles.get(5)).release();

System.out.println("left key released");

repaint();

}

};

ActionMap actionMap = this.getActionMap();

actionMap.put("right arrow pressed", actionRightArrowPressed);

actionMap.put("left arrow pressed", actionLeftArrowPressed);

actionMap.put("right arrow released", actionRightArrowRelease);

actionMap.put("left arrow released", actionLeftArrowRelease);

this.setActionMap(actionMap);

KeyStroke rightArrowPressed = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false);

InputMap inputMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

inputMap.put(rightArrowPressed, "right arrow pressed");

KeyStroke leftArrowPressed = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false);

inputMap.put(leftArrowPressed, "left arrow pressed");

KeyStroke rightArrowReleased = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true);

inputMap.put(rightArrowReleased, "right arrow released");

KeyStroke leftArrowReleased = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true);

inputMap.put(leftArrowPressed, "left arrow released");

JusteUneQuestiona at 2007-7-12 17:26:32 > top of Java-index,Desktop,Core GUI APIs...
# 5
Ok after rebuilding project and changing the mistake with my leftArrowPressed its working.Thanks for your help.Message was edited by: JusteUneQuestion
JusteUneQuestiona at 2007-7-12 17:26:33 > top of Java-index,Desktop,Core GUI APIs...
# 6
> Ok after rebuilding project and changing the mistake> with my leftArrowPressed its working.> Thanks for your help.your welcome. Glad you got it working./p
petes1234a at 2007-7-12 17:26:33 > top of Java-index,Desktop,Core GUI APIs...