Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyAdapter Masking Problem  RSS feed

Josh Rehman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that a KeyAdapter registered on a JFrame is masking a KeyAdapter registered on a JPanel contained within the JFrame. I seem to be unable to trap for KeyEvents from the JPanel.
I have an application which consists of a single frame and a cardlayout, which put's the application's panels on top. Normally, there is a KeyAdapter attached to the top frame, which traps for CTL+D and does something. I need one of the panels to trap CTL+D instead. I attempted to add a KeyListener to the panel, however it does not seem to be intercepting the keystrokes.
It appears that this is because the Panel does not have the focus, even though the components within it *do* have the focus. I say this becuase even when I change the key combo to a non-conflicting value (e.g. CTL+F) nothing appears to happen.
One solution is to add an API to the Panel that returns a KeyAdapter, and have the Frame obtain this KeyAdapter when the context changes. But this would replace the original KeyAdapter, rather than augment it. There are ways to design around this, but it seems messy to me.
Another solution is to use a KeyStroke/Action map. First, this would require re-writing old code. Second, I am not very familiar with this process. There is a helpful link at
however it isn't clear how this would work.
Has anyone encountered this issue before?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!