Java Swing GridLayout gridLayout(JComponent c, int rows, int cols, int gap, boolean n, boolean s, boolean w, boolean e)

Here you can find the source of gridLayout(JComponent c, int rows, int cols, int gap, boolean n, boolean s, boolean w, boolean e)

Description

grid Layout

License

Open Source License

Declaration

public static void gridLayout(JComponent c, int rows, int cols, int gap, boolean n, boolean s, boolean w,
            boolean e) 

Method Source Code

//package com.java2s;
/*//from   ww  w . j ava  2 s  . c  o  m
 * Swift Parallel Scripting Language (http://swift-lang.org)
 * Code from Java CoG Kit Project (see notice below) with modifications.
 *
 * Copyright 2005-2014 University of Chicago
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.JComponent;
import javax.swing.SpringLayout;

public class Main {
    public static void gridLayout(JComponent c, int rows, int cols, int gap, boolean n, boolean s, boolean w,
            boolean e) {
        SpringLayout sl = new SpringLayout();
        c.setLayout(sl);
        for (int row = 0; row < rows; row++) {
            if (n) {
                if (row == 0) {
                    sl.putConstraint(SpringLayout.NORTH, c.getComponent(0), 0, SpringLayout.NORTH, c);
                }
            }
            if (row != 0) {
                sl.putConstraint(SpringLayout.NORTH, c.getComponent(cols * row), gap, SpringLayout.SOUTH,
                        c.getComponent(cols * row - cols));
            }
            if (w) {
                sl.putConstraint(SpringLayout.WEST, c.getComponent(cols * row), 0, SpringLayout.WEST, c);
            }
            for (int col = 1; col < cols; col++) {
                sl.putConstraint(SpringLayout.WEST, c.getComponent(cols * row + col), gap, SpringLayout.EAST,
                        c.getComponent(cols * row + col - 1));
                sl.putConstraint(SpringLayout.SOUTH, c.getComponent(cols * row + col), 0, SpringLayout.SOUTH,
                        c.getComponent(cols * row + col - 1));
                if (row != 0) {
                    sl.putConstraint(SpringLayout.WEST, c.getComponent(cols * row + col), 0, SpringLayout.WEST,
                            c.getComponent(cols * (row - 1) + col));
                }
            }
            if (e) {
                sl.putConstraint(SpringLayout.EAST, c.getComponent(cols * row + cols - 1), 0, SpringLayout.EAST, c);
            }
            if (s) {
                if (row == rows - 1) {
                    sl.putConstraint(SpringLayout.SOUTH, c.getComponent(cols * row), 0, SpringLayout.SOUTH, c);
                }
            }
        }
    }
}

Related

  1. layoutEvenHorizontal(JComponent... list)