title: Java: Expand JButton to fill Container
body: <p>I have a <code>JButton</code> that I would like to expand to fill the size of a <code>JPanel</code> that is holding it. I've tried doing this a few different ways, with no luck. Here are some attempts:</p>
  
  <ol>
  <li><p>Manual size setting as recommended <a href="https://stackoverflow.com/questions/2536873/how-can-i-set-size-of-a-button">here</a> - size of the button did not change.</p>
  
  <pre><code>panel  = new JPanel(new CardLayout());
  button = new JButton();
  button.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE)); 
  panel.add(button);
  </code></pre></li>
  <li><p>Trying to use a <code>BorderLayout</code> to expand the button as hinted at <a href="https://stackoverflow.com/questions/311876/jbutton-expanding-to-take-up-entire-frame-container">here</a>. Size did not change.</p>
  
  <pre><code>panel  = new JPanel(new BorderLayout());
  button = new JButton();
  panel.add(button, BorderLayout.CENTER);
  </code></pre></li>
  </ol>
  
  <p>I'm probably doing something incorrectly, so any help is much appreciated.</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>Here is a summary of what resolved it. There are 2 things that worked </p>
  
  <ol>
  <li>Removing the LayoutManager (in the call to <code>new JPanel</code>), which <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html" rel="nofollow noreferrer">according to Oracle</a> defaults to BorderLayout.</li>
  <li>Adding the dimensions to the call as in the accepted answer (i.e. <code>new BorderLayout(0,0)</code>). </li>
  </ol>