# GridBagLayout - Position immer zentriert



## mr.mvp (11. Nov 2008)

Hallo,
ich hab ein Panel welches größer ist, als die enthaltenen Komponenten. Die Komponenten layoute ich mit einem GridBagLayout. Nun ist es leider so, dass der GridBagLayout-Manager immer zentriert (horizontal wie vertikal) im Panel platziert ist. Wie kann ich diese Position beeinflussen. Hab dazu leider nirgends etwas gefunden.

Danke
markus


----------



## Guest (11. Nov 2008)

Wohl an der falschen Stelle gesucht ;-) das hier sollte Dir weiter helfen.
API GridBagConstraints


----------



## L-ectron-X (11. Nov 2008)

Mit anchor kannst du die Ausrichtung einstellen. Mit weightx/weighty setzt du eine Gewichtung auf Komponenten.
Mit der Forumsuche findest du auch Beispielcode.


----------



## mr.mvp (11. Nov 2008)

Mit den Constraints hab ich schon viel rumprobiert. Diese ändern nur die Position der Komponente innerhalb einer Zelle, bzw. vergrößert der Abstand zwischen Zellenrand und Komponente. Was ich brauche ist, dass das gesamte Layout-Raster z.B. am oberen Rand des Panels ausgerichtet wird.

In meiner GUI verwende ich ein SplitPane. Die linke Seite legt die Höhe fest und die rechte Seite beinhaltet ein Panel, welches wiederum Komponenten enthält. Diese sollten am oberen Rand des SplitPanes ansetzen. Hab hier mal ein kleines Demo, der Übersichtlichkeit halber nicht mit einem SplitPane. Wie kann ich in diesem Fall die Buttons innerhalb des blauen Panels ausrichten (also. z.B. oben links)

Danke für eure Hilfe,
markus


```
/*
 * TestPanel.java
 *
 * Created on 7. Oktober 2008, 11:17
 */

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class TestPanel extends javax.swing.JPanel
{

    public static void main( String[] args )
    {
       JFrame mainFrame = new JFrame();
       TestPanel test = new TestPanel();
       mainFrame.getContentPane().add(test);
       mainFrame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE);
       mainFrame.pack();
       mainFrame.setVisible( true);
    }
           
    /** Creates new form TestPanel */
    public TestPanel()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      jPanel1 = new javax.swing.JPanel();
      jPanel2 = new javax.swing.JPanel();
      jButton1 = new javax.swing.JButton();
      jButton2 = new javax.swing.JButton();
      jButton3 = new javax.swing.JButton();
      jButton4 = new javax.swing.JButton();

      setLayout(new java.awt.GridLayout());

      jPanel1.setBackground(new java.awt.Color(255, 255, 51));
      jPanel1.setName("jPanel1"); // NOI18N

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(0, 186, Short.MAX_VALUE)
      );
      jPanel1Layout.setVerticalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(0, 181, Short.MAX_VALUE)
      );

      add(jPanel1);

      jPanel2.setBackground(new java.awt.Color(102, 102, 255));
      jPanel2.setName("jPanel2"); // NOI18N
      jPanel2.setLayout(new java.awt.GridBagLayout());

      jButton1.setText("jButton1");
      jButton1.setName("jButton1"); // NOI18N
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
      jPanel2.add(jButton1, gridBagConstraints);

      jButton2.setText("jButton2");
      jButton2.setName("jButton2"); // NOI18N
      jPanel2.add(jButton2, new java.awt.GridBagConstraints());

      jButton3.setText("jButton3");
      jButton3.setName("jButton3"); // NOI18N
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridy = 1;
      jPanel2.add(jButton3, gridBagConstraints);

      jButton4.setText("jButton4");
      jButton4.setName("jButton4"); // NOI18N
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      gridBagConstraints.gridy = 1;
      jPanel2.add(jButton4, gridBagConstraints);

      add(jPanel2);
   }// </editor-fold>

   // Variables declaration - do not modify
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton2;
   private javax.swing.JButton jButton3;
   private javax.swing.JButton jButton4;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JPanel jPanel2;
   // End of variables declaration
}
```


----------



## L-ectron-X (11. Nov 2008)

```
/*
* TestPanel.java
*
* Created on 7. Oktober 2008, 11:17
*/

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class TestPanel extends javax.swing.JPanel
{

    public static void main( String[] args )
    {
       JFrame mainFrame = new JFrame();
       TestPanel test = new TestPanel();
       mainFrame.getContentPane().add(test);
       mainFrame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE);
       mainFrame.pack();
       mainFrame.setVisible( true);
    }

    /** Creates new form TestPanel */
    public TestPanel()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      jPanel1 = new javax.swing.JPanel();
      jPanel2 = new javax.swing.JPanel();
      jButton1 = new javax.swing.JButton();
      jButton2 = new javax.swing.JButton();
      jButton3 = new javax.swing.JButton();
      jButton4 = new javax.swing.JButton();

      setLayout(new java.awt.GridLayout());

      jPanel1.setBackground(new java.awt.Color(255, 255, 51));
      jPanel1.setName("jPanel1"); // NOI18N

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(0, 186, Short.MAX_VALUE)
      );
      jPanel1Layout.setVerticalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(0, 181, Short.MAX_VALUE)
      );

      add(jPanel1);

      jPanel2.setBackground(new java.awt.Color(102, 102, 255));
      jPanel2.setName("jPanel2"); // NOI18N
      jPanel2.setLayout(new java.awt.GridBagLayout());

      jButton1.setText("jButton1");
      jButton1.setName("jButton1"); // NOI18N
      jPanel2.add(jButton1, new java.awt.GridBagConstraints());

      jButton2.setText("jButton2");
      jButton2.setName("jButton2"); // NOI18N
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
      jPanel2.add(jButton2, gridBagConstraints);

      jButton3.setText("jButton3");
      jButton3.setName("jButton3"); // NOI18N
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridy = 1;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
      gridBagConstraints.weighty = 1.0;
      jPanel2.add(jButton3, gridBagConstraints);

      jButton4.setText("jButton4");
      jButton4.setName("jButton4"); // NOI18N
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      gridBagConstraints.gridy = 1;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
      gridBagConstraints.weightx = 1.0;
      jPanel2.add(jButton4, gridBagConstraints);

      add(jPanel2);
   }// </editor-fold>

   // Variables declaration - do not modify
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton2;
   private javax.swing.JButton jButton3;
   private javax.swing.JButton jButton4;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JPanel jPanel2;
   // End of variables declaration
}
```


----------



## mr.mvp (12. Nov 2008)

Vielen Dank,
dann hab ich das Konzept wahrscheinlich noch nicht richtig verstanden 
Muss mir das nochmal genauer anschauen.

Gruß
markus


----------

