Java JComponent Size inset(Component c, int insetSizeHor, int insetSizeVert)

Here you can find the source of inset(Component c, int insetSizeHor, int insetSizeVert)

Description

Inset the given component by the given insetSize for horizontally and vertically.

License

Open Source License

Parameter

Parameter Description
c The component to wrap
insetSizeHor Inset horizontal
insetSizeVert Inset vertical

Return

A JPanel that contains the given component inset the specified amount.

Declaration

public static JPanel inset(Component c, int insetSizeHor, int insetSizeVert) 

Method Source Code

//package com.java2s;
/*/*from  w  w w  .  ja  v a  2 s . com*/
 * $Id: GuiUtils.java,v 1.317 2007/08/10 14:26:33 jeffmc Exp $
 *
 * Copyright  1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library 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 library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.*;

import java.util.ArrayList;

import java.util.Hashtable;

import java.util.List;

import javax.swing.*;

public class Main {
    /** Used by the doLayout routine for the default layout insets */
    private static final Insets DFLT_INSETS = new Insets(0, 0, 0, 0);
    /**
     * If you want to change the insets used in the doLayout routines
     * you can set this and the routine will use the value and then set it to null.
     * I know, I know, it isn't thread safe but...
     */
    public static Insets tmpInsets;
    /**
     * Use this to define your own column fills (i.e., horizontal expansion
     * of the elements in the gribag). Will be set to null after use.
     * Not thread safe but...
     */
    public static int[] tmpColFills = null;
    /** This is the default anchor used in doLayout */
    private static final int DFLT_ANCHOR = GridBagConstraints.WEST;
    /**
     * Set this to define your own anchor in the doLayout routines.
     * Will get reset after use. Not thread safe. You can also
     * call {@link #setAnchorBottom()} and {@link #setAnchorTop()} for
     * changing the anchor.
     */
    public static int tmpAnchor = -1;
    /**
     * Set this to define your own fill in the doLayout routines.
     * Will get reset after use. Not thread safe. You can also
     * call {@link #setNoFill()} and {@link #setHFill()} for doing
     * no fill and horizontal fill.
     */
    public static int tmpFill = -1;
    /**
     * All of the WT_  members are used to define the column and row
     * weights  for the doLayout routines.  They are double arrays,
     * typically one for each col or row, that hold either a 1 or a 0.
     * The "Y" and "N" implies YES or NO, i.e.,
     * does the corresponding row/column get weight (Y) or not get weight (N).
     * <p>
     * So for example, if you wanted to have no stretchiness in the first column
     * and put all of the stretchiness into the second column you would use:<pre>
     * WT_NY</pre>
     * <p>
     * Note, you can pass in your own double arrays to doLayout. These are just here
     * for convenience.
     */
    public static final double[] WT_Y = { 1 };
    /** doLayout weights */
    public static final double[] WT_N = { 0 };

    /**
     * Inset the given component by the given insetSize, both
     * horizontally  and vertically.
     *
     * @param c The component to wrap
     * @param insetSize Horizontal and vertical inset
     * @return A JPanel that contains the given component inset the specified amount.
     */
    public static JPanel inset(Component c, int insetSize) {
        return inset(c, insetSize, insetSize);
    }

    /**
     * Inset the given component by the given insetSize for
     * horizontally  and vertically.
     *
     * @param c The component to wrap
     * @param insetSizeHor Inset horizontal
     * @param insetSizeVert Inset vertical
     * @return A JPanel that contains the given component inset the specified amount.
     */
    public static JPanel inset(Component c, int insetSizeHor, int insetSizeVert) {
        return doLayout(new JPanel(), new Component[] { c }, 1, WT_Y, WT_Y, null, null,
                new Insets(insetSizeVert, insetSizeHor, insetSizeVert, insetSizeHor));
    }

    /**
     * Inset the given component by the given insets.
     *
     * @param c The component to wrap
     * @param insets The insets
     * @return A JPanel that contains the given component inset the specified amount.
     */
    public static JPanel inset(Component c, Insets insets) {
        return doLayout(new JPanel(), new Component[] { c }, 1, WT_Y, WT_Y, null, null, insets);
    }

    /**
     * Inset the given component, adding it into the
     * given parent container, by the given insetSize, both
     * horizontally  and vertically.
     *
     * @param parent The panel to put the component in.
     * @param c The component to wrap
     * @param insetSize Horizontal and vertical inset
     * @return A JPanel that contains the given component inset the specified amount.
     */
    public static JPanel inset(JPanel parent, Component c, int insetSize) {
        return doLayout(parent, new Component[] { c }, 1, WT_Y, WT_Y, null, null,
                new Insets(insetSize, insetSize, insetSize, insetSize));
    }

    /**
     * This does a column oriented grid bag layout. It will layout
     * the given components in a grid with the given number of columns.
     *
     * @param components List of {@link Component}s to layout
     * @param numberOfColumns How many columns in the grid layout
     * @param hinset hor. inset
     * @param vinset vert. inset
     * @return THe new panel
     */
    public static JPanel doLayout(List components, int numberOfColumns, int hinset, int vinset) {
        return doLayout(new JPanel(), getComponentArray(components), numberOfColumns, WT_N, WT_N, null, null,
                new Insets(hinset, vinset, hinset, vinset));
    }

    /**
     * This does a column oriented grid bag layout. It will layout
     * the given components in a grid with the given number of columns.
     *
     * @param components The  components to layout
     * @param numberOfColumns How many columns in the grid layout
     * @param weightsX Defines how much weight to give to each column width. If there are more
     * columns than weights then we use the last weight.
     * @param weightsY Defines how much weight to give to each row height. If there are more
     * rows than weights then we use the last weight.
     * @return New panel
     */
    public static JPanel doLayout(List components, int numberOfColumns, double[] weightsX, double[] weightsY) {
        return doLayout(new JPanel(), getComponentArray(components), numberOfColumns, weightsX, weightsY, null,
                null, null);
    }

    /**
     * This does a column oriented grid bag layout. It will layout
     * the given components in a grid with the given number of columns.
     *
     * @param components The  components to layout
     * @param numberOfColumns How many columns in the grid layout
     * @param weightsX Defines how much weight to give to each column width. If there are more
     * columns than weights then we use the last weight.
     * @param weightsY Defines how much weight to give to each row height. If there are more
     * rows than weights then we use the last weight.
     * @return New panel
     */
    public static JPanel doLayout(Component[] components, int numberOfColumns, double[] weightsX,
            double[] weightsY) {
        return doLayout(new JPanel(), components, numberOfColumns, weightsX, weightsY, null, null, null);
    }

    /**
     * This does a column oriented grid bag layout. It will layout
     * the given components in a grid with the given number of columns.
     *
     * @param parentContainer The container to add components to. May be null.
     * @param components The  components to layout
     * @param numberOfColumns How many columns in the grid layout
     * @param weightsX Defines how much weight to give to each column width. If there are more
     * columns than weights then we use the last weight.
     * @param weightsY Defines how much weight to give to each row height. If there are more
     * rows than weights then we use the last weight.
     * @return New panel
     */
    public static JPanel doLayout(JPanel parentContainer, Component[] components, int numberOfColumns,
            double[] weightsX, double[] weightsY) {
        return doLayout(parentContainer, components, numberOfColumns, weightsX, weightsY, null, null, null);
    }

    /**
     * This does a column oriented grid bag layout. It will layout
     * the given components in a grid with the given number of columns.
     * It is probably good to read up a bit on
     * <a href=http://java.sun.com/j2se/1.3/docs/api/java/awt/GridBagLayout.html>GridBagLayout</a>
     * <p>
     * The weights define how much weight or spacing to give to the width
     * of each column and the height of each row.
     * <p>
     * To define the anchor value, i.e., how to fill a component in its grid square,
     * you can either set the global static member <code>tmpAnchor</code> or, for individual
     * Components you can use the anchors table to provide a mapping from Component to an
     * Integer object holding the anchor value.
     * <p>
     * To define the fill value, i.e., how a component expands in its grid square,
     * you can either set the global static member <code>tmpFill</code> or, for individual
     * Components you can use the fills table to provide a mapping from Component to an
     * Integer object holding the fill value.
     * <p>
     * If insets is non-null it will use those insets for the spacing in the grid.
     * else if the static member tmpInsets is non-null then it will use those values
     * and then set tmpInsets to null. Else it uses DFLT_INSETS, which is 0 spacing.
     * <p>
     *
     *
     * @param parentContainer The container to add components to. May be null.
     * @param components The  components to layout
     * @param numberOfColumns How many columns in the grid layout
     * @param weightsX Defines how much weight to give to each column width. If there are more
     * columns than weights then we use the last weight.
     * @param weightsY Defines how much weight to give to each row height. If there are more
     * rows than weights then we use the last weight.
     * @param anchors Hashtable that maps Component to the Integer  which defines the component anchor
     * @param fills Hashtable that maps Component to the Integer  which defines the component fill
     * @param insets The insets to use in the grid
     * @return The parentContainer or the new panel if parentContainer is null
     */
    public static JPanel doLayout(JPanel parentContainer, Component[] components, int numberOfColumns,
            double[] weightsX, double[] weightsY, Hashtable anchors, Hashtable fills, Insets insets) {

        if (parentContainer == null) {
            parentContainer = new JPanel();
        }

        //TODO: When we move to 1.6 we need to remove this fix
        //Check if we've blown the size limit for gridbag
        if (components.length > 512) {
            //Not perfect but...
            Component[] comps1 = new Component[components.length / 2];
            Component[] comps2 = new Component[components.length / 2 + 1];
            int cnt = 0;
            for (int i = 0; i < components.length; i++) {
                if (i < comps1.length) {
                    comps1[i] = components[i];
                } else {
                    comps2[cnt++] = components[i];
                }
            }
            JComponent comp1 = doLayout(null, comps1, numberOfColumns, weightsX, weightsY, anchors, fills, insets);
            JComponent comp2 = doLayout(null, comps2, numberOfColumns, weightsX, weightsY, anchors, fills, insets);

            return vbox(comp1, comp2);
        }

        GridBagLayout l = new GridBagLayout();
        parentContainer.setLayout(l);

        GridBagConstraints consts = new GridBagConstraints();
        if (insets == null) {
            insets = tmpInsets;
            tmpInsets = null;
        }
        consts.insets = ((insets == null) ? DFLT_INSETS : insets);

        int[] dfltColFills = null;
        if (tmpColFills != null) {
            dfltColFills = tmpColFills;
        }
        tmpColFills = null;

        int dfltAnchor = ((tmpAnchor == -1) ? DFLT_ANCHOR : tmpAnchor);
        tmpAnchor = -1;
        int dfltFill = ((tmpFill >= 0) ? tmpFill : GridBagConstraints.BOTH);
        tmpFill = -1;

        int col = 0;
        int row = 0;

        double weightX = 1.0;
        double weightY = 0.0;

        for (int i = 0; i < components.length; i++) {
            Component comp = components[i];
            consts.anchor = dfltAnchor;

            consts.fill = dfltFill;
            if ((fills != null) && (comp != null)) {
                Integer fill = (Integer) fills.get(comp);
                if (fill != null) {
                    consts.fill = fill.intValue();
                }
            } else if (dfltColFills != null) {
                consts.fill = dfltColFills[col];
            }

            if ((weightsX != null) && (col < weightsX.length)) {
                weightX = weightsX[col];
            }

            if ((weightsY != null) && (row < weightsY.length)) {
                weightY = weightsY[row];
            }

            boolean lastCol = false;

            if (col == (numberOfColumns - 1)) {
                lastCol = true;
                consts.gridwidth = GridBagConstraints.REMAINDER;
            } else {
                col++;
            }
            consts.weightx = weightX;
            consts.weighty = weightY;

            if ((anchors != null) && (comp != null)) {
                Integer anchor = (Integer) anchors.get(comp);
                if (anchor != null) {
                    consts.anchor = anchor.intValue();
                }
            }
            if (lastCol) {
                col = 0;
                row++;
            }

            if (comp != null) {
                l.setConstraints(comp, consts);
                parentContainer.add(comp);
            }
            consts.gridwidth = 1;
        }
        return parentContainer;

    }

    /**
     * A utility to convert the listOfComponents into a Component  array
     *
     * @param listOfComponents List of Components
     * @return Component  array
     */
    public static Component[] getComponentArray(List listOfComponents) {
        Component[] c = new Component[listOfComponents.size()];
        for (int i = 0; i < listOfComponents.size(); i++) {
            c[i] = (Component) listOfComponents.get(i);
        }
        return c;
    }

    /**
     * Layout the given components vertically.
     *
     * @param c1 Component 1
     * @param c2 Component 2
     * @return The new container of the components.
     */
    public static JPanel vbox(Component c1, Component c2) {
        return vbox(toList(new Object[] { c1, c2 }));
    }

    /**
     * Layout the given components vertically.
     *
     * @param c1 Component 1
     * @param c2 Component 2
     * @param c3 Component 3
     * @return The new container of the components.
     */
    public static JPanel vbox(Component c1, Component c2, Component c3) {
        return vbox(toList(new Object[] { c1, c2, c3 }));
    }

    /**
     * Layout the given components vertically.
     *
     * @param c1 Component 1
     * @param c2 Component 2
     * @param c3 Component 3
     * @param c4 Component 4
     * @return The new container of the components.
     */
    public static JPanel vbox(Component c1, Component c2, Component c3, Component c4) {
        return vbox(toList(new Object[] { c1, c2, c3, c4 }));
    }

    /**
     * Layout the given components vertically.
     *
     * @param components The components
     * @return The new container of the components.
     */
    public static JPanel vbox(Component[] components) {
        return vbox(toList(components));
    }

    /**
     * Layout the given components vertically.
     *
     * @param components The components
     * @return The new container of the components.
     */
    public static JPanel vbox(List components) {
        return vbox(new JPanel(), components);
    }

    /**
     * Layout the given components vertically.
     *
     * @param components The components
     * @param panel The panel  to put the components in if on-null.
     * @return The given panel if non-null, else the newly create panel.
     */
    public static JPanel vbox(JPanel panel, List components) {
        return doLayout(panel, getComponentArray(components), 1, WT_Y, WT_N);
    }

    public static List toList(Object[] l) {
        ArrayList v = new ArrayList();
        for (int i = 0; i < l.length; i++) {
            v.add(l[i]);
        }
        return v;
    }
}

Related

  1. getSize(Component c, int sizeflag)
  2. getValidatedSize(Component c, int sizeflag)
  3. getViewableScreenSize()
  4. getVisibleSizeInViewport(Component c)
  5. getWindowSize(JComponent start)
  6. isSmallSizeVariant(JComponent c)
  7. makeEqualSize(JComponent reference, JComponent... others)
  8. makeSameSize(Component... components)
  9. makeSameSize(JComponent... comps)