GirdBagLayout -> How to make the buttons fill vertically

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 fill vertically

Postby Karl » Tue Jun 09, 2009 1:40 am

Here comes a simple example,
and I do not know how to make the JToggleButtons
use all the vertical space.
Can anybody help me?

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 toggleButton = new JToggleButton();
      toggleButton.setText("New JToggleButton");
      final GridBagConstraints gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.fill = GridBagConstraints.VERTICAL;
      panel_4.add(toggleButton, gridBagConstraints);

      final JToggleButton toggleButton_1 = new JToggleButton();
      final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
      gridBagConstraints_1.fill = GridBagConstraints.VERTICAL;
      panel_4.add(toggleButton_1, gridBagConstraints_1);
      toggleButton_1.setText("New JToggleButton");

      final JToggleButton toggleButton_2 = new JToggleButton();
      toggleButton_2.setText("New JToggleButton");
      final GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
      gridBagConstraints_2.fill = GridBagConstraints.VERTICAL;
      panel_4.add(toggleButton_2, gridBagConstraints_2);

      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 fill vertically

Postby Eric Clayberg » Tue Jun 09, 2009 4:33 am

You can set the rowWeight of that row to 1.0 or you can set the weightY of the buttons to 1.0. For example,

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


Code: Select all
      gridBagConstraints.weighty = 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