If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

KeyListeners in JPanels using a CardLayout

I'm making and testing a swing application using key listeners and CardLayout. I have two JPanels that can respond to key events; one counts and displays the number of key presses so far, the other displays what key was last pressed. I only want one of these panels visible at a time, and I want key presses to only affect the visible panel.

I contain these two panels in a third JPanel that uses a CardLayout. This panel itself has a key listener to switch between the panels it's containing. I stick this panel in a JFrame for the final application.

What I'm trying to do in general is make a JPanel that acts as a container for two other JPanels, only showing one at a time. If I press a certain key, like SPACE for example, the contained panels are switched so that the hidden one is visible and the visible one is hidden. If some other key is pressed, I want that key to affect and be processed by the visible panel, but not the hidden one.

I sort of having it work the way I want right now, except I have to press TAB a lot. When I'm flipping between the two "inner" panels I have to press TAB before my key presses affect it. I have to press TAB again to be able to flip between my two inner panels again.

I sort of having it work the way I want right now, except I have to press TAB a lot. When I'm flipping between the two "inner" panels I have to press TAB before my key presses affect it. I have to press TAB again to be able to flip between my two inner panels again.

I'm not really sure what you mean... so hitting the spacebar doesn't work until you hit tab? and you completely lost me on the second half of that quote.

So you got a CardPanel containing 2 MyPanel's, but only want 1 panel visible at a time? Only add 1 panel to CardPanel then. When user presses "space", remove the current panel and add the 2nd one instead, and visa-versa.

I'm not really sure what you mean... so hitting the spacebar doesn't work until you hit tab? and you completely lost me on the second half of that quote.

I mean when I first start the application, switching the panels with the spacebar works fine. But when I press another key (other than tab), the panel currently visible isn't affected like it should be. It's only when I hit tab that key presses have an effect on the visible panel. But now I'm unable to switch panels with the spacebar anymore, until I hit tab again, where it goes back to what it's like when I first started the application. Sorry if this doesn't clear things up.

So you got a CardPanel containing 2 MyPanel's, but only want 1 panel visible at a time? Only add 1 panel to CardPanel then. When user presses "space", remove the current panel and add the 2nd one instead, and visa-versa.

If I'd just removing and inserting panels instead of just flipping between them, should my outer panel even be using a CardLayout? The whole reason I'm using a CardLayout is because I want to store multiple panels but only have one visible and active at a time.

Well in Windows, TAB switches focus to the next component. My guess is that after switching, the panel no longer has the focus, and when you hit TAB, the focus is being set on the next component (in this case, your panel).