Utility common in use with GridBagLayout. - Java Swing

Java examples for Swing:GridBagLayout

Description

Utility common in use with GridBagLayout.

Demo Code

/*//from   w w w.  j  av a 2s. c o m
 * Sshtools - Java SSH2 API
 *
 * Copyright (C) 2002 Lee David Painter.
 *
 * Written by: 2002 Lee David Painter <lee@sshtools.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
//package com.java2s;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;

public class Main {
    /**
     * Utility common in use with GridBagLayout. Adds a component to the
     * specified grid width position
     *
     * @param parent container
     * @param componentToAdd Description of the Parameter
     * @param constraints object
     * @param pos Description of the Parameter
     *
     * @throws IllegalArgumentException if parent is not a JComponent
     */
    public static void jGridBagAdd(JComponent parent,
            JComponent componentToAdd, GridBagConstraints constraints,
            int pos) {
        if (!(parent.getLayout() instanceof GridBagLayout)) {
            throw new IllegalArgumentException(
                    "parent must have a GridBagLayout");
        }

        //
        GridBagLayout layout = (GridBagLayout) parent.getLayout();

        //
        constraints.gridwidth = pos;
        layout.setConstraints(componentToAdd, constraints);
        parent.add(componentToAdd);
    }
}

Related Tutorials