title: JPanel layout in applet
body: <p>I have a JPanel that is not part of a JFrame. For various reasons I have to call the panel's paint method through my own "update" method.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public void onLoad ()
  {
      panel = new JPanel ();
      panel.setBounds (0,0,Main.WIDTH,Main.HEIGHT);
  
      panel.setLayout (new BoxLayout (panel, BoxLayout.Y_AXIS));
  
      addButton ("button1", panel);
      addButton ("button2", panel);
  }
  
  private void addButton (String text, Container container)
  {
      JButton button = new JButton (text);
      button.setPreferredSize (new Dimension (100,20));
      button.setAlignmentX (Component.CENTER_ALIGNMENT);
      container.add (button);
  }
  
  public void onRender (Graphics2D g)
  {
      panel.paint (g);
  }
  </code></pre>
  
  <p>This only paints the panel's background color. If I add button.setBounds(...) in the addButton method then it does paint the buttons but not affected by the BoxLayout.</p>
  
  <p>So I want the buttons to be affected by the BoxLayout obviously. I'm not that savvy on how exactly Swing works so I'm not sure how to do this. JFrame has a pack() method which I think is what I need but some equivalent for JPanels since JPanels doesn't have that method.</p>