Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Swing: Switching between components in application

Hi. I'm trying to figure out how to make a certain swing interface I have in mind. Generally what I want is a JPanel which acts as a container for at least two other JPanels, with only one of these panels visible at a time. All of these inner panels would be able to recieve key events, though as I said only one panel would be visible and enabled at a time.

The thing is I want to be able to switch from one inner panel to another with a keypress or some other action. For example, say I have two panels called title and work. When title is visible, I want it so that if I press a key, say ENTER, title is hidden and work is shown. I'd also want to switch from work to title, pressing ESCAPE for example. I might also have a third inner panel, say preferences, which I get to from work by pressing P, and returning to work from preferences with ESCAPE.

I'm sort of under the impression that I could use a CardLayout for my outer panel. The question is how to affect it from inside the inner panels. I've thought a little about making a subclass of Action which are generated by the inner panels and listened to by the outer panel; to tell the outer panel which inner panel to switch to. I don't know enough yet about Actions to figure out how to do that though.

If someone could give me a few pointers to how I might accomplish what I've described I'd be appreciative.

Thanks,

Aaron J. M.
[1479 byte] By [AaronJM] at [2007-11-11 7:37:03]