Greg Humiston
Greg Humiston

Reputation: 21

Setting multiple cards in CardLayout to False all at once

I understand the concept that with Card Layout for Swing, to switch cards, you can simply call the card you want and set the visibility to either true or false. For example, given four cards named a, b, c, and d, I want a to be displayed but b,c,and d to not be displayed. For now, I have my code set similar to this:

a.setVisible(true);
b.setVisible(false);
c.setVisible(false);
d.setVisible(false);

Is there anyway that instead of having to call all four cards and set their visibility individually that I can call all at once, set them to false, and then set Card a to true? For example

//some method to set all cards to false
a.setVisible(true);

Any help would be appreciated!

Upvotes: 0

Views: 140

Answers (2)

Sage
Sage

Reputation: 15418

I don't know why do you even need such option with CardLayout. It has previous, next and first function to traverse the component like flipping a card. Check out this demo example to see CardLayout in action. If you still need to have such option for your own reason, why not trying to get the CardContainer component list:

Component[] comp = cardContainer.getComponents();
comp[0].setVisible(false);

Upvotes: 1

camickr
camickr

Reputation: 324128

I understand the concept that with Card Layout for Swing, to switch cards, you can simply call the card you want and set the visibility to either true or false

No, that is not the concept. You never play with the visibility of the panels. The CardLayout does that for you. All you do is use the show(), next() or previous() methods.

Read the section from the Swing tutorial on How to Use Card Layout for a working example.

Upvotes: 2

Related Questions