Java JPanel Layout makeCompactGrid(JPanel jMainGridPanel, SpringLayout layout, int rows, int cols, int initialX, int initialY, int xPad, int yPad)

Here you can find the source of makeCompactGrid(JPanel jMainGridPanel, SpringLayout layout, int rows, int cols, int initialX, int initialY, int xPad, int yPad)

Description

Taken verabatim from java.sun.com online tutorial for using the SpringLayout to make a grid

License

Open Source License

Parameter

Parameter Description
jMainGridPanel panel to lay elemnts out into
layout the SpringLayour for the panel
rows number of rows
cols number of columns
initialX starting x position
initialY starting y position
xPad horizonatal padding between cells
yPad vertical padding between cells

Declaration

public static void makeCompactGrid(JPanel jMainGridPanel, SpringLayout layout, int rows, int cols, int initialX,
        int initialY, int xPad, int yPad) 

Method Source Code

//package com.java2s;
/****** BEGIN LICENSE BLOCK *****
 * This file is part of the Strainer application. To obtain or learn more 
 * about strainer visit: //from www  .  ja v  a  2 s.com
 *  http://bioinformatics.org/strainer
 * 
 * Copyright (c) 2007 The Regents of the University of California.  All rights
 * reserved.
 *
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as published 
 * by the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version. You may not use this file except in 
 * compliance with the License. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
 * USA or visit http://www.gnu.org/licenses/lgpl.html
 *
 * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
 * DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 * 
 * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
 * DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
 * IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 *
 * Contributor(s):
 *  John Eppley <jmeppley@berkeley.edu>
 * 
 ***** END LICENSE BLOCK ***** */

import javax.swing.JPanel;
import javax.swing.Spring;
import javax.swing.SpringLayout;

public class Main {
    /**
     * Taken verabatim from java.sun.com online tutorial for using the
     * SpringLayout to make a grid
     * 
     * @param jMainGridPanel
     *            panel to lay elemnts out into
     * @param layout
     *            the SpringLayour for the panel
     * @param rows
     *            number of rows
     * @param cols
     *            number of columns
     * @param initialX
     *            starting x position
     * @param initialY
     *            starting y position
     * @param xPad
     *            horizonatal padding between cells
     * @param yPad
     *            vertical padding between cells
     * @author 
     *         http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html
     */
    public static void makeCompactGrid(JPanel jMainGridPanel, SpringLayout layout, int rows, int cols, int initialX,
            int initialY, int xPad, int yPad) {
        // Align all cells in each column and make them the same width.
        Spring x = Spring.constant(initialX);
        for (int c = 0; c < cols; c++) {
            Spring width = Spring.constant(0);
            for (int r = 0; r < rows; r++) {
                width = Spring.max(width,
                        layout.getConstraints(jMainGridPanel.getComponent(r * cols + c)).getWidth());
            }
            for (int r = 0; r < rows; r++) {
                SpringLayout.Constraints constraints = layout
                        .getConstraints(jMainGridPanel.getComponent(r * cols + c));
                constraints.setX(x);
                constraints.setWidth(width);
            }
            x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
        }

        // Align all cells in each row and make them the same height.
        Spring y = Spring.constant(initialY);
        for (int r = 0; r < rows; r++) {
            Spring height = Spring.constant(0);
            for (int c = 0; c < cols; c++) {
                height = Spring.max(height,
                        layout.getConstraints(jMainGridPanel.getComponent(r * cols + c)).getHeight());
            }
            for (int c = 0; c < cols; c++) {
                SpringLayout.Constraints constraints = layout
                        .getConstraints(jMainGridPanel.getComponent(r * cols + c));
                constraints.setY(y);
                constraints.setHeight(height);
            }
            y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
        }

        // Set the parent's size.
        SpringLayout.Constraints pCons = layout.getConstraints(jMainGridPanel);
        pCons.setConstraint(SpringLayout.SOUTH, y);
        pCons.setConstraint(SpringLayout.EAST, x);
    }
}

Related

  1. constrain(Component component, JPanel panel, GridBagLayout layout, GridBagConstraints constraints, int gridx, int gridwidth, int weightx, int gridy, int gridheight, int weighty, int fill, int anchor)
  2. doLayout(JPanel parentContainer, Component[] components, int numberOfColumns, double[] weightsX, double[] weightsY)
  3. formGridInColumn(JPanel panel, int rows, int cols)
  4. getJPanel(LayoutManager man)
  5. layoutDualPanel(JPanel bean, JLabel[] labels, JComponent[] components, JLabel tLabel, JComponent tComp, JLabel rLabel, JComponent rComp)
  6. setBoxXLayout(JPanel p)
  7. setGBLayout(JPanel panel, int[] columnWidths, int[] rowHeights, double[] columnWeights, double[] rowWeights)
  8. setLayoutForEditingIcons(JPanel parentPanel, JLabel icon)
  9. showCentered(JPanel panel)