In my apllication i am using a jpanel in which i want to add a key listener. I did it. But it doesnot work.
Is it because i am using a swingworker to update the contents of the panel every second.
Here is my code to update the panel
RenderedImage image = ImageIO.read(new ByteArrayInputStream((byte[]) get()));
Graphics graphics = remote.rdpanel.getGraphics();
if (graphics != null) {
Image readyImage = new ImageIcon(UtilityFunctions.convertRenderedImage(image)).getImage();
graphics.drawImage(readyImage, 0, 0, remote.rdpanel.getWidth(), remote.rdpanel.getHeight(), null);
}
Does the
JPanelhave keyboard focus?I suggest you use the
InputMapandWHEN_IN_FOCUSED_WINDOWor something similar. Excerpt from How to Use Key Bindings:That has worked very robustly for me. Have a look at my other post for more information and actual code examples:
or this tutorial: Swing: Understanding Input/Action Maps
Related question: