Reputation: 329
I have created a class which has a panel named cards and its layout is CardLAyout. I have added card items. In this class I want to create a separate method by calling which, layout switches to next card.
import java.awt.CardLayout;
import java.awt.Container;
public class cards
{
public Container cards;
//creating objects for other classes
public cricGUI gu;
public cricMainMenu mm;
public void cardsList()
{
cards = new Container();
cards.setLayout(new CardLayout());
//adding panels and contentPanes from other classes.
mm = new cricMainMenu();
gu = new cricGUI();
cards.add(mm.contentPane);
cards.add(gu.pane);
}
public void getNextCard(Container x)
{
}
}
So as u can see I have panels on other classes that I have added to my cards. what i want to do is create getNextCard() method which takes the currently active panel as its arguements. when i call this function it should switch the currently active panel with the next one in my CardLayout list. How can i do this? Thanks
Upvotes: 3
Views: 1989
Reputation: 6178
You may want to have a look at How to use CardLayout.
Basic principle is, that every card gets its own identifier (usually a String Constant). To switch to a specific card, you call
layout.show( container, identifier );
To implement a method like getNextCard()
(better name would probably be switchToNextCard( container, identifier )
) you could for example use an easy switch case construct like:
public void switchToNextCard( Panel container, String currentCard )
{
switch ( currentCard )
{
case CARD1:
layout.show( container, CARD2 );
break;
case CARD2:
layout.show( container, CARD1 );
break;
default :
throw IllegalArgumentException("Unsupported CardIdentifier.")
break;
}
}
In this Method CARD1 and CARD2 are your identifiers (String Constants) for your panels within your cardlayout. In this case it would switch back and forward between those two cards.
Upvotes: 3