JPanel not listening to key event when there is a child component with JButton on it

Posted by Yifu on Stack Overflow See other posts from Stack Overflow or by Yifu
Published on 2010-05-08T05:09:37Z Indexed on 2010/05/08 5:18 UTC
Read the original article Hit count: 243

Filed under:
|
|
|

I'm working on a map editor for my college project. And I had a problem that the map panel is not listening key event while it should. This happens when I add a ToolBarPane (which extends JPanel) with JComponent such as JButton, JComboBox that implements ActionListener on it AND the map panel (which extends the JPanel) together on to the Frame (I used BorderLayout). I have System.out.println statement to test if the key press is received, and it's not printing, if I remove the ToolBar, the key listener works again, so is the mouseListenner is disabled just like the keyListener, which means I can't handle press events etc, but the mouseListener works fine and I can still handle mouse move event.

Here is a screen shot how it works without the ToolBarPane

http://img684.imageshack.us/img684/3232/sampleku.png

note that you can use the mouse to put images on the map, you can also select images using the mouse just like a laser tool, and by pressing number key you can switch between different images, this works fine until I add the ToolBarPane which shows like this:

img291.imageshack.us/img291/8020/failve.png (please add http before that, i can only post one hyperlink)

(I can't post images here cuz im a new user)

With the ToolBarPane on I was no longer able to handle the key event.

I guess it might by that the focus as been transfered to that panel somehow, but not sure at all.

Does and body know this and can help me out?

Thanks very much

© Stack Overflow or respective owner

Related posts about keylistener

Related posts about java