title: Need a GridBagLayout tutor for dummies please... with working example
body: <p>I'm trying to create a GUI that has a <code>JXTaskPane</code>, within which an HTML editor pane is showing some lengthy text, then below that are two sliders. One should have the full window width divided 50/50 between the label and the slider, the next slider should have 1/3rd of the width for the label and 2/3 for the slider.</p>
  
  <p>In order for this example to run, you need SwingX, <a href="https://java.net/downloads/swingx/releases/swingx-all-1.6.4.jar" rel="nofollow">download the jar here</a>.</p>
  
  <p>There are several problems with the code:</p>
  
  <ol>
  <li>On the first startup, the elements are not drawn correctly. The window needs to be resized, or the task pane closed and reopened. Can anyone recommend a way to fix this?</li>
  <li>Even though the <code>GridBagConstraint</code> fill parameter is set to use the full horizontal width, the components are only using up about half of the window width and are drawn in the center. How can I make them use the full width and height of the <code>JXTaskPane</code>?</li>
  <li>Even though <code>slider2</code> has its <code>gridwidth</code> property set to 2 (the label has it set to 1) it in fact is drawn narrower than its label! Why does it do that? I tried playing with the <code>weightx</code> parameter, but that just changes the appearance randomly to a little wider or a little narrower, but in what seems to be a random and rather unpredictable fashion. How do I make this label 1/3rd the width of its slider?</li>
  </ol>
  
  <hr>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import org.jdesktop.swingx.JXTaskPane;
  
  class test {
    public static void main(String[] arg) {
      JFrame mainWindow = new JFrame();
      JXTaskPane taskPane = new JXTaskPane();
      JSlider slider1 = new JSlider(0,100,50);
      JSlider slider2 = new JSlider(0,100,50);
  
      taskPane.setCollapsed(false);
  
      // slider 1 with its label in a simple GridLayout
      JPanel pnlSlider1 = new JPanel();
      pnlSlider1.setLayout(new GridLayout(1,1));  // 1 row, 1 column
      pnlSlider1.add(new JLabel("Description for slider1"));
      pnlSlider1.add(slider1);
  
      // slider 2 with its label is in a GridBagLayout, the label should be 1/3 of the width of the slider
      JPanel pnlSlider2 = new JPanel();
      pnlSlider2.setLayout(new GridBagLayout());
      GridBagConstraints c2 = new GridBagConstraints();
      c2.fill = GridBagConstraints.HORIZONTAL;
      c2.gridx = 0;
      c2.gridy = 0;
      c2.gridwidth = 1;
      c2.weighty = 1;
      c2.weightx = 1;
      pnlSlider2.add(new JLabel("Description for slider2"), c2);
      c2.gridx = 1;
      c2.gridwidth = 2;
      c2.weightx = 1;
      pnlSlider2.add(slider2, c2);
  
      // label should now be to the left of slider
  
      String content = "&lt;html&gt;Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content ";
      content += "Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content";
      content += "Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content &lt;/html&gt;";
      JEditorPane ep = new JEditorPane("text/html", content);
  
      // this is the main window panel
      //JPanel panel = new JPanel();
      taskPane.setLayout(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      c.fill = GridBagConstraints.HORIZONTAL;
      c.gridx = 0;
      c.gridy = 0;
      taskPane.add(ep, c);
      c.gridy = 1;
      taskPane.add(pnlSlider1, c);
      c.gridy = 2;
      c.insets = new Insets(10,0,0,0);
      taskPane.add(pnlSlider2, c);
  
      // tie it all together and display the window
          mainWindow.setPreferredSize(new Dimension(600, 600));
          mainWindow.setLocation(100, 100);
          mainWindow.getContentPane().add(taskPane);
          mainWindow.pack();
          mainWindow.addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                  System.exit(0);
              }
          });
          mainWindow.setVisible(true);
  
    }
  }
  </code></pre>