I am attempting to develop a LARP character manager, and I have inside my frame, a panel to contain all the windows I want to swap through using CardLayout. Here is my code for the ContainerPane.
public class ContainerPane extends JPanel {
private static final long serialVersionUID = -4799973935806714569L;
private JPanel deckOfPanes = null;
private PlayerManagerPane myPlayerManagerPane = null;
private GameManagerPane myGameManagerPane= null;
private CharacterManagerPane myCharacterManagerPane = null;
final static String CHANGETOCHARACTERMANAGERPANE = "Character Manager";
final static String CHANGETOPLAYERMANAGERPANE = "Player Manager";
final static String CHANGETOGAMEMANAGERPANE = "Game Manager";
public ContainerPane(EventListener myEventListener) {
myPlayerManagerPane = new PlayerManagerPane(myEventListener);
myGameManagerPane = new GameManagerPane(myEventListener);
myCharacterManagerPane = new CharacterManagerPane(myEventListener);
deckOfPanes= new JPanel(new CardLayout());
deckOfPanes.add(myCharacterManagerPane,CHANGETOCHARACTERMANAGERPANE);
deckOfPanes.add(myPlayerManagerPane,CHANGETOPLAYERMANAGERPANE);
deckOfPanes.add(myGameManagerPane,CHANGETOGAMEMANAGERPANE);
CardLayout cardLayout = (CardLayout) ((ContainerPane) this).getDeckOfPanes().getLayout();
cardLayout.show(deckOfPanes,CHANGETOCHARACTERMANAGERPANE);
}
public JPanel getDeckOfPanes() {
return deckOfPanes;
}
To start, I'd imagine that the constructor's final line would ensure that when it is called it display's a certain card on top.
Elsewhere in my code I want to swap the cards around, using a menu bar. Here is the code from my EventHandler class:
public void swapView(String key) {
CardLayout cardLayout = (CardLayout) ((ContainerPane) myContainerPane).getDeckOfPanes().getLayout();
cardLayout.show(myContainerPane.getDeckOfPanes(),key);
}
This isn't working either. I'm just starting Java and I'd really appreciate any help with this, I've check the tutorials and elsewhere on the web (including stack overflow) and from what I can see, it should be working. Please, any help would be appreciated.
You have not added deckOfPanes to your ContainerPane. Add:
deckOfPanes = new JPanel(cardLayout);
add(deckOfPanes);
// etc.