I've been searching for a solution for a while now and despite all the similar questions and answers, found nothing that seems to work. I want a user to be able to progress through various panels set up in a card layout. However, I want the buttons used to switch between these cards to be on the cards themselves, not a separate set of buttons which doesn't change throught the program. Here is the main file where the frame is created:
public class BattleGraphs_V1
{
JPanel cards;
public int cardNumber = 1;
public void addComponentToPane (Container pane)
{
JPanel MainMenuCard = new MainMenu_V1();
JPanel DifficultySelectorCard = new DifficultySelector_V1();
cards = new JPanel(new CardLayout());
cards.add(MainMenuCard);
cards.add(DifficultySelectorCard);
pane.add(cards, BorderLayout.CENTER);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("BattleGraphs");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new java.awt.Dimension(725, 420));
frame.setResizable(false);
BattleGraphs_V1 containerPanel = new BattleGraphs_V1();
containerPanel.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
The two panels (more in the future) are in two seperate files in the project. I haven't added the code for them as apart from an action listener for the single button on each panel, it was all auto-generated code.
EDIT: I added an auto-generated action listener to the button to the MainMenu JPanel like so:
private void MainMenuCardSBActionPerformed(java.awt.event.ActionEvent evt) {
BattleGraphs_V1 BG_V1 = new BattleGraphs_V1();
CardLayout cardLayout = (CardLayout) (BG_V1.cards.getLayout());
cardLayout.show(BG_V1.cards, "DifficultyCard");
}
After having added names to the cards added in the main file (main file being BattleGraphs_V1) like so:
cards.add(MainMenuCard, "MainMenuCard");
cards.add(DifficultySelectorCard, "DifficultyCard");
And when I ran the program and clicked the button got a null pointer exception error, as I have done with similar solutions. Would I be wrong in assuming this is more down to an issue of scope?
2nd EDIT: Right, hopefully this should help a little, I've added the enite project to a GitHub repository - https://github.com/charga600/BattleGraphs/tree/master