title: Java function for building GUI panel
body: <p>I'm building a GUI layout for my application using the grid bag and I'm trying to come up with a function to layout each element so that I don't have to keep re typing the same grid bag code over and over. I want to rewrite this code:</p>
  
  <pre><code>GridBagLayout gridbag = new GridBagLayout();
  GridBagConstraints bc = new GridBagConstraints();
  this.setLayout(gridbag);
  
  bc.fill = GridBagConstraints.HORIZONTAL;
  bc.anchor = GridBagConstraints.WEST;
  bc.insets = new Insets(0, 10, 10, 0);
  bc.gridx = 0;
  bc.gridy = 0;
  bc.gridwidth = 1;
  this.add(programNameLabel, bc);
  </code></pre>
  
  <p>so that it can be written calling a function like this:</p>
  
  <pre><code>labelPosition(GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 0, 10, 10, 0, 0, 0, 1, programNameLabel);
  </code></pre>
  
  <p>Here is the function that I have written for the task.</p>
  
  <pre><code>protected void labelPosition(int axis, int direction, int insetOne, int insetTwo, int insetThree, int insetFour, int gridX, int gridY, int gridWidth, JLabel name)
      {
          GridBagLayout gridbag = new GridBagLayout();
          GridBagConstraints bc = new GridBagConstraints();
          this.setLayout(gridbag);
  
          bc.fill = axis;
          bc.anchor = direction;
          bc.insets = new Insets(insetOne, insetTwo, insetThree, insetFour);
          bc.gridx = gridX;
          bc.gridy = gridY;
          bc.gridwidth = gridWidth;
          this.add(name, bc);
      }
  </code></pre>
  
  <p>Now it compiles, but when I run it, it doesn't work. All the labels are displayed in a single line rather than the layout that I'm looking for. </p>
  
  <p>Is what I'm trying to do simply possible or am I missing something in my code?? Any suggestions?</p>