title: Dynamically Add Components to a JDialog
body: <p>I am having trouble adding JComponents to a JDialog when the user clicks a button on the JDialog. Basically I want it to look like this:</p>
  
  <p><img src="https://i.stack.imgur.com/hXqdA.png" alt="When the dialog is opened"></p>
  
  <p>Then, when the user clicks "Add New Field" I want it to look like this:</p>
  
  <p><img src="https://i.stack.imgur.com/mkmYV.png" alt="After the user clicks &quot;Add New Field&quot;"></p>
  
  <p>I cannot seem to get the dialog to add the new JLabel or JTextField. Can anyone point me in the right direction?</p>
  
  <p><strong>EDIT</strong>: This is the action for the "Add New Field" button (Just trying a label now).</p>
  
  <pre><code>@Action
  public void addNewField()
  {
      Container contentPane = getContentPane();
      JLabel label = new JLabel ("welkom");
      contentPane.add(label, BorderLayout.CENTER);
  }
  </code></pre>
  
  <p><strong>SOLUTION</strong>:</p>
  
  <p>I used mre's solution and got it to work. Here is my final function:</p>
  
  <pre><code>@Action
  public void addNewField()
  {
      System.out.println("New Field...");
      Container contentPane = getContentPane();
      JLabel label = new JLabel ("welcome");
      label.setBounds(10,10,100,10); //some random value that I know is in my dialog
      contentPane.add(label);
  
      contentPane.validate();
      contentPane.repaint();
      this.pack();
  }
  </code></pre>
  
  <p>Another one of my problems is that I am using a "Free Design" layout in NetBeans, which meant that my label was probably in some weird position rather than being in the bounds of my dialog (just a guess). I solved this problem with <code>label.setBounds()</code> so that it showed exactly where I wanted it to.</p>