title: Creating a dynamic frame/ Adding a panel to a frame during runtime
body: <p>I'm very much a rookie to java and I am trying to make a GUI client which once a button is pressed, then a new panel will be added to the frame. In this instance, when the a button called createMessage is pressed then the following panel will be added to the main frame.</p>
  
  <pre><code>    JPanel createMessagePanel = new JPanel();
  
      createMessagePanel.setLayout(new GridLayout(8, 0, 5, 10));
  
      messageText = new JTextArea(5, 20);
  
      sendTo = new JTextField("Recipient"); //Declared Global
  
      JLabel createMessagePrompt = new JLabel("CREATE MESSAGE");
      JLabel userPrompt = new JLabel("Send to:");
      JLabel messagePrompt = new JLabel("Message:");
  
      sendButton = new JButton("Send!");
      createAMessageButton.addActionListener(this);
  
      createMessagePanel.add(createMessagePrompt);
      createMessagePanel.add(userPrompt);
      createMessagePanel.add(sendTo);
      createMessagePanel.add(messagePrompt);
      createMessagePanel.add(new JScrollPane(messageText));
      createMessagePanel.add(sendButton);
  
      add(createMessagePanel, BorderLayout.CENTER);
  </code></pre>
  
  <p>This sadly doesn't work, I've been told that this may be down to that the frame needs to know about the components added to the frame at runtime. How would I be able to achieve this effect of upon a button pressed then different panels which contains other components would be displayed in the position BorderLayout.CENTER.</p>
  
  <p>Thanks in advance.</p>