Java JComponent Width addComponent(Container container, JComponent component, int xPos, int yPos, int width, int height, double weightX, double weightY, int insetTop, int insetLeft, int insetBottom, int insetRight, int anchor, int stretch)

Here you can find the source of addComponent(Container container, JComponent component, int xPos, int yPos, int width, int height, double weightX, double weightY, int insetTop, int insetLeft, int insetBottom, int insetRight, int anchor, int stretch)

Description

Define the parameter for the gridbaglayout constraints

License

Open Source License

Parameter

Parameter Description
container Container where to place the component
component Component to place
xPos X position in the grid
yPos Y position in the grid
compWidth Component width
compHeight Component height
weightX X weight (1=can be resized in X)
weightY Y weight (1=can be resized in Y)
insetTop Inner top space in pixel
insetLeft Inner left space in pixel
insetBottom Inner bottom space in pixel
insetRight Inner right space in pixel
anchor Position of the component in the "cell"
stretch How the component will fill the "cell"

Declaration

public static void addComponent(Container container, JComponent component, int xPos, int yPos, int width,
        int height, double weightX, double weightY, int insetTop, int insetLeft, int insetBottom,
        int insetRight, int anchor, int stretch) 

Method Source Code

//package com.java2s;
/*/*  www  . j  a v a2 s  . co  m*/
 * Course Generator
 * Copyright (C) 2016 Pierre Delore
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Container;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JComponent;

public class Main {
    /**
     * Define the parameter for the gridbaglayout constraints
     * 
     * @param container
     *            Container where to place the component
     * @param component
     *            Component to place
     * @param xPos
     *            X position in the grid
     * @param yPos
     *            Y position in the grid
     * @param compWidth
     *            Component width
     * @param compHeight
     *            Component height
     * @param weightX
     *            X weight (1=can be resized in X)
     * @param weightY
     *            Y weight (1=can be resized in Y)
     * @param insetTop
     *            Inner top space in pixel
     * @param insetLeft
     *            Inner left space in pixel
     * @param insetBottom
     *            Inner bottom space in pixel
     * @param insetRight
     *            Inner right space in pixel
     * @param anchor
     *            Position of the component in the "cell"
     * @param stretch
     *            How the component will fill the "cell"
     */
    public static void addComponent(Container container, JComponent component, int xPos, int yPos, int width,
            int height, double weightX, double weightY, int insetTop, int insetLeft, int insetBottom,
            int insetRight, int anchor, int stretch) {

        GridBagConstraints gridConstraints = new GridBagConstraints();

        gridConstraints.gridx = xPos;
        gridConstraints.gridy = yPos;
        gridConstraints.gridwidth = width;
        gridConstraints.gridheight = height;
        gridConstraints.weightx = weightX;
        gridConstraints.weighty = weightY;
        gridConstraints.insets = new Insets(insetTop, insetLeft, insetBottom, insetRight);
        gridConstraints.anchor = anchor;
        gridConstraints.fill = stretch;

        container.add(component, gridConstraints);

    }
}

Related

  1. adjustNarrowField(final JComponent component, final int width)
  2. adjustWidth(JComponent aComponent, int aWidth)
  3. allignBottom(JComponent lead, JComponent target, int width, int height)
  4. calculateCharWidth(JComponent component, int numChars)