I've been looking through the documentation for the Swing CardLayout and there doesn't appear to be any way to determine which card is currently showing baked in to the class. Yet there must be a way to ask the layout which card it is currently showing, right?
Due to project constraints, I can't simply extend it and add this functionality to a sub-class, so if there isn't such a function does that mean I'm stuck tracking the component's state external to the component (yuck!), or is there some other option buried somewhere deep in Swing?
According to the How to Use CardLayout tutorial,
Conceptually, each component that a CardLayout manages is like a playing card or trading card in a stack, where only the top card is visible at any time. You can choose the card that is showing in any of the following ways:
By asking for either the first or last card, in the order it was added to the containerBy flipping through the deck backwards or forwardsBy specifying a card with a specific name
Try,
Component visibleComponent = foo.getComponent(0);
where foo
is the JPanel
instance using CardLayout
.
Reference:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With