GridLayout
Home ] Up ] FlowLayout ] LayoutManager ] LayoutManager2 ] Components & Containers ] BorderLayout ] [ GridLayout ] Insets ] Nested Layouts ]

 

Rows and Columns
GridLayout Gaps

 

Some would claim that GridLayout is perhaps the easiest layout manager to use.  

For example:

package swingExamples;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class GridButtons1Panel extends JPanel
{
  public GridButtons1Panel()
  {
    setBackground(Color.lightGray);
    setLayout( new GridLayout(1, 3) ); // rows, cols
    add(m_button1);
    add(m_button2);
    add(m_button3);
  }
  
  ////////////// Data //////////////////
  private JButton m_button1 = new JButton("1");
  private JButton m_button2 = new JButton("2");
  private JButton m_button3 = new JButton("3");
}

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

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

which produces:

Note in particular that every cell in a GridLayout panel is the same size as every other cell.

 

This page was last modified on 02 October, 2007