title: Adding JPanel on another JPanel on button press
body: <p>I have created a <code>MainPanel</code> in which I make a bird fly. To this panel, I have to add a <code>PausePanel</code> panel at the press of a button. If I only use <code>pausePanel.setVisible (true / false)</code> when pressing the key, this works. But, if the bird starts before the first keypress, the panel will never be displayed.</p>
  <p>I, therefore, thought of adding and removing the <code>PausePanel</code> every time I press the key. Unfortunately, the game pauses but the <code>PausePanel</code> does not appear.</p>
  <p>Here's the code:</p>
  <p><strong>PausePanel:</strong></p>
  <pre class="lang-java prettyprint-override"><code>public class PausePanel extends JPanel {
  
      /**
       * 
       */
      private static final long serialVersionUID = -3943280654281528696L;
      private final JLabel resumeLabel = new JLabel(&quot;ESC: RESUME&quot;, SwingConstants.CENTER);
      private final JLabel exitLabel = new JLabel(&quot;ENTER: EXIT THE GAME&quot;, SwingConstants.CENTER);
      private final MainFontLoader fontLoader = new MainFontLoader();
      private final Font gameFont = fontLoader.load();
  
      public PausePanel() {
          exitLabel.setFont(gameFont);
          resumeLabel.setFont(gameFont);
          resumeLabel.setVerticalAlignment(JLabel.BOTTOM);
          exitLabel.setVerticalAlignment(JLabel.NORTH);
          resumeLabel.setForeground(Color.WHITE);
          exitLabel.setForeground(Color.WHITE);
          
          this.setBackground(Color.BLACK);
          this.setLayout(new GridLayout(0, 1));
          this.add(resumeLabel);
          this.add(exitLabel);
          this.setVisible(false);
      }
      
      public boolean getVisibility() {
          return this.isVisible();
      }
  }
  </code></pre>
  <p>Here I need the <code>getVisibility</code> method for another <code>ExitButton</code> that should only work when this panel is visible.</p>
  <p><strong>PauseButton:</strong></p>
  <pre class="lang-java prettyprint-override"><code>public class PauseButton {
  
      private boolean pause = false;
      
      public PauseButton(JPanel mainPanel, JPanel pausePanel, BirdVisual visual) {
  
          final KeyStroke escKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
          mainPanel.getInputMap().put(escKey, &quot;Esc&quot;);
          mainPanel.getActionMap().put(&quot;Esc&quot;, new AbstractAction(){
              
          private static final long serialVersionUID = -534997922777581539L;
  
          @Override
          public void actionPerformed(ActionEvent e) {
              setVisibility(mainPanel, pausePanel, visual);
          }
      });
  }
  
      public void setVisibility(JPanel mainPanel, JPanel pausePanel, BirdVisual visual) {
          if(!pause){
              mainPanel.add(pausePanel);
              pausePanel.setVisible(true);
              pausePanel.revalidate();
              pausePanel.repaint();
              visual.setPause();
              pause = true;
          } else {
              mainPanel.remove(pausePanel);
              pausePanel.setVisible(false);
              visual.setPause();
              pause = false;
          }
      }
  }
  </code></pre>
  <p><strong>MainPanel:</strong></p>
  <pre class="lang-java prettyprint-override"><code>public class MainPanel extends JPanel{
      
      /**
       * 
       */
      private static final long serialVersionUID = -5567586066880648121L;
      private PausePanel pausePanel = new PausePanel();
      private final BirdVisual visual = new BirdVisual(this);
  
      
      public MainPanel() {
          this.setBackground(Color.WHITE);
          this.setLayout(null);
          new PauseButton(this, pausePanel, visual);
          new ExitButton(this, pausePanel);
      }
  
      public BirdVisual getVisual() {
          return this.visual;
      }
  
  }
  </code></pre>