/* Copyright (C) 2002 J. M. Spivey */ import java.awt.*; /** A layout manager that maintains a grid, but with variable * column widths and row heights. Components must be added by * rows in left-to-right order. * * Implemented as a specialized version of GridBagLayout, * with (thankfully) a simple interface. */ public class VariGridLayout extends GridBagLayout { private int nrows, ncols; private int row = 0, col = 0; private GridBagConstraints gbc = new GridBagConstraints(); private Insets insets; /** Construct a layout that is nrows x ncols */ public VariGridLayout(int nrows, int ncols) { this(nrows, ncols, 0); } /** Construct a layout that is nrows x ncols, * with same inset all round. */ public VariGridLayout(int nrows, int ncols, int inset) { this.nrows = nrows; this.ncols = ncols; insets = new Insets(inset, inset, inset, inset); gbc.fill = GridBagConstraints.BOTH; gbc.insets = insets; } /** Add another component in row-by-row order */ public void addLayoutComponent(Component c, Object cns) { gbc.gridx = col; gbc.gridy = row; super.addLayoutComponent(c, gbc); col++; if (col == ncols) { col = 0; row++; } } }