Java JPanel Child addToGridBag(Component comp, JPanel panel, int gridx, int gridy, int width, int height, int anchor)

Here you can find the source of addToGridBag(Component comp, JPanel panel, int gridx, int gridy, int width, int height, int anchor)

Description

Adds a component to a panel with a grid bag layout

License

Open Source License

Parameter

Parameter Description
comp a parameter
panel a parameter
gridx a parameter
gridy a parameter
width a parameter
height a parameter
anchor a parameter

Declaration

public static void addToGridBag(Component comp, JPanel panel, int gridx, int gridy, int width, int height,
        int anchor) 

Method Source Code

//package com.java2s;
/**/* w  w w  . java  2s .co m*/
 * DataCleaner (community edition)
 * Copyright (C) 2014 Neopost - Customer Information Management
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

import java.awt.Component;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;

import javax.swing.JPanel;

public class Main {
    public static final int DEFAULT_PADDING = 2;
    public static final int DEFAULT_ANCHOR = GridBagConstraints.NORTHWEST;

    /**
     * Adds a component to a panel with a grid bag layout
     * 
     * @param comp
     * @param panel
     * @param gridx
     * @param gridy
     * @param width
     * @param height
     * @param anchor
     */
    public static void addToGridBag(Component comp, JPanel panel, int gridx, int gridy, int width, int height,
            int anchor) {
        addToGridBag(comp, panel, gridx, gridy, width, height, anchor, DEFAULT_PADDING);
    }

    public static void addToGridBag(Component comp, JPanel panel, int gridx, int gridy, int width, int height,
            int anchor, int padding) {
        addToGridBag(comp, panel, gridx, gridy, width, height, anchor, padding, 0.0, 0.0);
    }

    /**
     * Adds a component to a panel with a grid bag layout
     * 
     * @param comp
     * @param panel
     * @param gridx
     * @param gridy
     * @param width
     * @param height
     * @param anchor
     * @param padding
     */
    public static void addToGridBag(Component comp, JPanel panel, int gridx, int gridy, int width, int height,
            int anchor, int padding, double weightx, double weighty) {
        LayoutManager layout = panel.getLayout();
        if (!(layout instanceof GridBagLayout)) {
            layout = new GridBagLayout();
            panel.setLayout(layout);
        }
        GridBagLayout gridBagLayout = (GridBagLayout) layout;
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = gridx;
        constraints.gridy = gridy;
        constraints.gridwidth = width;
        constraints.gridheight = height;
        constraints.weightx = weightx;
        constraints.weighty = weighty;
        constraints.anchor = anchor;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(padding, padding, padding, padding);
        gridBagLayout.addLayoutComponent(comp, constraints);
        panel.add(comp);
    }

    /**
     * Adds a component to a panel with a grid bag layout
     * 
     * @param comp
     * @param panel
     * @param gridx
     * @param gridy
     * @param width
     * @param height
     */
    public static void addToGridBag(Component comp, JPanel panel, int gridx, int gridy, int width, int height) {
        addToGridBag(comp, panel, gridx, gridy, width, height, DEFAULT_ANCHOR);
    }

    public static void addToGridBag(Component comp, JPanel panel, int x, int y, int anchor, double weightx,
            double weighty) {
        addToGridBag(comp, panel, x, y, 1, 1, anchor, DEFAULT_PADDING, weightx, weighty);
    }

    /**
     * Adds a component to a panel with a grid bag layout
     * 
     * @param comp
     * @param panel
     * @param gridxs
     * @param gridy
     */
    public static void addToGridBag(Component comp, JPanel panel, int gridx, int gridy) {
        addToGridBag(comp, panel, gridx, gridy, 1, 1);
    }

    public static void addToGridBag(Component comp, JPanel panel, int gridx, int gridy, double weightx,
            double weighty) {
        addToGridBag(comp, panel, gridx, gridy, 1, 1, DEFAULT_ANCHOR, DEFAULT_PADDING, weightx, weighty);
    }

    /**
     * Adds a component to a panel with a grid bag layout
     * 
     * @param comp
     * @param panel
     * @param gridx
     * @param gridy
     * @param anchor
     */
    public static void addToGridBag(Component comp, JPanel panel, int gridx, int gridy, int anchor) {
        addToGridBag(comp, panel, gridx, gridy, 1, 1, anchor);
    }
}

Related

  1. addGBC(JPanel panel, Component component, int gridx, int gridy, int anchor, Insets insets)
  2. addInput2Pane(String label, Component c, JPanel p, int row)
  3. addSection(JPanel panel, String title)
  4. addSpacer(JPanel panel)
  5. addTo(JPanel panel, String label, String value)
  6. addToGridBag(final JPanel panel, final Component component, final int x, final int y)
  7. addTwoCheckBoxes(JPanel panel, JCheckBox first, boolean firstChecked, JCheckBox second, boolean secondChecked)
  8. addUIComponent(JPanel jp, JComponent jc, int loc, int type, int[] decart, Insets insets)
  9. addWidth(JPanel panel, int width)