GridBagLayout
Home ] Up ] BoxLayout ] [ GridBagLayout ] SpringLayout ]

 

 

GridBagLayout is by far the most difficult to use of the standard layout managers.  It is incredibly painful to use, and very picky -- the smallest mistake can mess things up royally!.  

In order to use a GridBagLayout, you also must use a GridBagConstraints.

Here's an example of the use of GridBagLayout:

package swingExamples;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

class FontLayoutPanel extends JPanel 
    implements ActionListener, ListSelectionListener
{
  public FontLayoutPanel()
  {
    setLayout(new GridBagLayout());
    setBackground(Color.pink);
    GridBagConstraints gbc = new GridBagConstraints();
    
    m_fontList.setSelectedIndex(0);
    m_fontList.addListSelectionListener(this);
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.0;
    gbc.weighty = 1.0;
    add(m_fontList, gbc, 0, 0, 1, 3);
    
    m_bold.addActionListener(this);
    m_bold.setBackground(Color.green);
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.CENTER;
    add(m_bold, gbc, 1, 0, 2, 1);
    
    m_italic.addActionListener(this);
    m_italic.setBackground(Color.cyan);
    add(m_italic, gbc, 1, 1, 2, 1);
    
    m_sizeLabel.setBackground(Color.yellow);
    add(m_sizeLabel, gbc, 1, 2, 1, 1);
    
    m_size.addActionListener(this);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(m_size, gbc, 2, 2, 1, 1);
    
    m_sample.setBackground(Color.magenta);
    m_sample.setEditable(false);
    m_sample.setText("The quick brown fox");
    gbc.anchor = GridBagConstraints.SOUTH;
    gbc.weighty = 0.0;
    add(m_sample, gbc, 0, 3, 4, 1);
  }
  
  private void add(Component comp, 
                   GridBagConstraints gbc,
                   int x, int y, 
                   int width, int height)
  {
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = width;
    gbc.gridheight = height;
    add(comp, gbc);
  }
  
  public void actionPerformed(ActionEvent evt)
  {
    updateFont();
  }
  
  public void valueChanged(ListSelectionEvent evt)
  {
    updateFont();
  }
  
  private void updateFont()
  {
    int style = 0;
    if (m_bold.isSelected())
    {
      style = Font.BOLD;
    }
    if (m_italic.isSelected())
    {
      style += Font.ITALIC;
    }
    Font font = new Font( 
        (String) m_fontList.getSelectedValue(),
        style,
        Integer.parseInt(m_size.getText())
        );
    m_sample.setFont(font);
    repaint();
  }
  
  ////// Private data //////
  private static final String[] m_fonts =
  {
    "Serif", "SansSerif", "Monospaced", 
    "Dialog", "DialogInput"
  };
  private JList m_fontList = new JList(m_fonts);
  private JCheckBox m_bold = new JCheckBox("Bold");
  private JCheckBox m_italic = new JCheckBox("Italic");
  private JLabel m_sizeLabel = new JLabel("Size: ");
  private JTextField m_size = new JTextField("10", 2);
  private JTextField m_sample = new JTextField();
}

class FontLayoutFrame extends JFrame
{
  public FontLayoutFrame()
  {
    setTitle("FontLayout");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.add( new FontLayoutPanel() );
  }
}

public class GridBagLayoutExample
{
  public static void main(String[] args)
  {
    FontLayoutFrame frame = new FontLayoutFrame();
    frame.setVisible(true);
  }
}

which produces:

Note that the (rather garish!) colors are meant to show you the boundaries of the various components.

 

This page was last modified on 02 October, 2007