GirdBagLayout -> How to make the buttons move west

Swing Designer allows you to quickly create the frames, panels, dialogs, applets and other UI elements that comprise Java Swing applications.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

GirdBagLayout -> How to make the buttons move west

Postby Karl » Mon Jun 15, 2009 7:51 am

Dear Eric Clayberg,

thanks, for all your patient help so far,
it was really useful for me.

Dear Folks,

here another question, in the example below are two buttons,
within a GridBagLayout, it seams to me as if they are centered
within their containing JPanel. How can I make them move further
left or west. So as if they line up "left to right", using all the vertical
space (Y-height) of the containing JPanel. But not using all the space
right of them (X-width). In other words there should be some free space
right or east of them, because not all space is needed.

JToggleButton + JToggleButton + FreeSpace

How can I do this?

Thanks in advance,

Code: Select all

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.Box;
import javax.swing.BoxLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class FillVertically extends JFrame {

    * Launch the application
    * @param args
   public static void main(String args[]) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
               FillVertically frame = new FillVertically();
            } catch (Exception e) {

    * Create the frame
   public FillVertically() {
      setBounds(100, 100, 500, 375);

      final JPanel panel = new JPanel();
      panel.setLayout(new BorderLayout());

      final JPanel panel_2 = new JPanel();
      panel_2.setPreferredSize(new Dimension(0, 55));
      panel.add(panel_2, BorderLayout.NORTH);

      final JPanel panel_1 = new JPanel();
      panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));

      final Component component = Box.createRigidArea(new Dimension(24, 0));

      final JPanel panel_4 = new JPanel();
      panel_4.setLayout(new GridBagLayout());

      final JToggleButton moveMeLeftToggleButton = new JToggleButton();
      moveMeLeftToggleButton.setText("move me left 1");
      final GridBagConstraints gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.weightx = 0;
      gridBagConstraints.weighty = 1.0;
      gridBagConstraints.fill = GridBagConstraints.VERTICAL;
      gridBagConstraints.anchor = GridBagConstraints.WEST;
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridy = 0;
      panel_4.add(moveMeLeftToggleButton, gridBagConstraints);

      final JToggleButton moveMeLeftToggleButton_1 = new JToggleButton();
      moveMeLeftToggleButton_1.setText("move me left 2");
      final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
      gridBagConstraints_1.gridy = 0;
      gridBagConstraints_1.gridx = 1;
      gridBagConstraints_1.anchor = GridBagConstraints.WEST;
      gridBagConstraints_1.fill = GridBagConstraints.VERTICAL;
      panel_4.add(moveMeLeftToggleButton_1, gridBagConstraints_1);

      final Component component_1 = Box.createRigidArea(new Dimension(24, 0));

      final JPanel panel_3 = new JPanel();
      panel_3.setPreferredSize(new Dimension(0, 80));
      panel.add(panel_3, BorderLayout.SOUTH);

Posts: 9
Joined: Tue Jun 02, 2009 12:51 am

Re: GirdBagLayout -> How to make the buttons move west

Postby Eric Clayberg » Mon Jun 15, 2009 9:16 am

Set the column weight on the secont column to 1.0...

Code: Select all
      GridBagLayout gridBagLayout = new GridBagLayout();
      gridBagLayout.columnWeights = new double[]{0.0, 1.0};

...or set weigthX for the second button...

Code: Select all
      gridBagConstraints_1.weightx = 1.0;
Eric Clayberg
Software Engineering Manager

Author: "Eclipse Plug-ins"
Eric Clayberg
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Return to Swing Designer

Who is online

Users browsing this forum: No registered users and 1 guest