Java JPanel Layout 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)

Here you can find the source of 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)

Description

Adds the component and sets its grid bag constraints.

License

Open Source License

Parameter

Parameter Description
component The component.
panel The panel.
layout The grid bag layout.
gridx The grid x position.
gridwidth The grid width.
weightx How much extra horizontal space the component will get.
gridy The grid y position.
gridheight The grid height.
weighty How much extra vertical space the component will get.
fill Whether the component should expand to fill its space.
anchor Where to place the component if it doesn't fill its space.

Declaration

public static void 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) 

Method Source Code

//package com.java2s;
// The source code contained herein is licensed under the IBM Public License   

import javax.swing.JLabel;
import javax.swing.JPanel;

import java.awt.Component;

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

public class Main {
    /**//w w w .j  ava 2  s .com
     Adds the component and sets its grid bag constraints.

     @param  component   The component.
     @param  panel       The panel.
     @param  layout      The grid bag layout.
     @param  gridx       The grid x position.
     @param  gridwidth   The grid width.
     @param  weightx     How much extra horizontal space the component will get.
     @param  gridy       The grid y position.
     @param  gridheight  The grid height.
     @param  weighty     How much extra vertical space the component will get.
     @param  fill        Whether the component should expand to fill its space.
     @param  anchor      Where to place the component if it doesn't fill its space.
     **/
    public static void 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) {
        panel.add(component);

        constraints.gridx = gridx;
        constraints.gridy = gridy;
        constraints.gridwidth = gridwidth;
        constraints.gridheight = gridheight;
        constraints.fill = fill;
        constraints.anchor = anchor;
        constraints.weightx = weightx;
        constraints.weighty = weighty;

        layout.setConstraints(component, constraints);
    }

    /**
     Adds the component and sets its grid bag constraints.

     @param  component   The component.
     @param  panel       The panel.
     @param  layout      The grid bag layout.
     @param  gridx       The grid x position.
     @param  gridy       The grid y position.
     @param  gridwidth   The grid width.
     @param  gridheight  The grid height.
     **/
    public static void constrain(Component component, JPanel panel,
            GridBagLayout layout, int gridx, int gridy, int gridwidth,
            int gridheight) {
        panel.add(component);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = gridx;
        constraints.gridy = gridy;
        constraints.gridwidth = gridwidth;
        constraints.gridheight = gridheight;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.ipady = 2;
        constraints.insets = new Insets(2, 0, 2, 0);
        constraints.anchor = GridBagConstraints.WEST;
        constraints.weightx = 1;
        constraints.weighty = 1;

        layout.setConstraints(component, constraints);
    }

    /**
     Adds 2 component as a row and sets its grid bag constraints.

     @param  component   The left component.
     @param  component2  The right component.
     @param  panel       The panel.
     @param  layout      The grid bag layout.
     @param  gridy       The grid y position.
     **/
    public static void constrain(Component component, Component component2,
            JPanel panel, GridBagLayout layout, int gridy) {
        constrain(component, panel, layout, 0, gridy, 1, 1);
        constrain(component2, panel, layout, 1, gridy, 1, 1);
    }

    /**
     Adds 2 strings as a row and sets its grid bag constraints.

     @param  string1     The left component.
     @param  string2     The right component.
     @param  panel       The panel.
     @param  layout      The grid bag layout.
     @param  gridy       The grid y position.
     **/
    public static void constrain(String string1, String string2,
            JPanel panel, GridBagLayout layout, int gridy) {
        constrain(new JLabel(string1), new JLabel(string2), panel, layout,
                gridy);
    }

    /**
     Adds the component as a row and sets its grid bag constraints.

     @param  component   The component.
     @param  panel       The panel.
     @param  layout      The grid bag layout.
     @param  gridy       The grid y position.
     **/
    public static void constrain(Component component, JPanel panel,
            GridBagLayout layout, int gridy) {
        constrain(component, panel, layout, 0, gridy, 2, 1);
    }
}

Related

  1. doLayout(JPanel parentContainer, Component[] components, int numberOfColumns, double[] weightsX, double[] weightsY)
  2. formGridInColumn(JPanel panel, int rows, int cols)
  3. getJPanel(LayoutManager man)
  4. layoutDualPanel(JPanel bean, JLabel[] labels, JComponent[] components, JLabel tLabel, JComponent tComp, JLabel rLabel, JComponent rComp)