title: Singleton with CardLayout won't show card when another class calls Singleton.instance.show()
body: <pre><code>public class MainWindow extends JPanel {
  
  public static MainWindow instance = new MainWindow();
  
  private CardLayout cards = new CardLayout();
  
  public MainWindow() {
      setLayout(cards);
      add(new FirstPage(), Pages.FIRST.toString());
      add(new SecondPage(), Pages.SECOND.toString());
      add(new ThirdPage(), Pages.THIRD.toString());
  }
  
  public void showPage(Pages page) {
      cards.show(this, page.toString());
  }
  
  }
  </code></pre>
  
  <p>the <code>showPage(page);</code> method works fine if I call it in the constructor of <code>MainWindow</code>. But when I try to call <code>MainWindow.instance.showPage(Pages.SECOND);</code> from an ActionListener in <code>FirstPage</code> nothing happens. I've checked that the <code>showPage(page)</code> method works correctly. I've checked that the ActionEvent is fired and enters the correct if/else clause. What am I doing wrong, why isn't my second page showing?</p>
  
  <pre><code>public class FirstPage extends JPanel {
  
      private JButton showSecond = new JButton("Show Second");
      private JButton showThird = new JButton("Show Third");
  
      public FirstPage() {
          insertButton(showSecond);
          insertButton(showThird);
      }
  
      private void insertButton(JButton button) {
          button.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  if (e.getSource() == showSecond) {
                      MainWindow.instance.showPage(Pages.SECOND);
                  } else {
                      MainWindow.instance.showPage(Pages.THIRD);
                  }
              }
          });
          this.add(button);
      }
  }
  </code></pre>