Java Swing BoxLayout hbox(Component[] components, int spacing)

Here you can find the source of hbox(Component[] components, int spacing)

Description

Do a horizontal layout of the given components.

License

Open Source License

Parameter

Parameter Description
components The components
spacing _more_

Return

The new container of the components

Declaration

public static JPanel hbox(Component[] components, int spacing) 

Method Source Code

//package com.java2s;
/*/*www . j a  va  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.List;

import javax.swing.*;

public class Main {
    /**
     * Do a horizontal layout of the given components with the given spacing.
     *
     * @param c1 Component 1
     * @param c2 Component 2
     * @param spacing Inter component spacing
     * @return The new container of the components
     */
    public static JPanel hbox(Component c1, Component c2, int spacing) {
        return hbox(new JPanel(), toList(new Object[] { c1, c2 }), spacing);
    }

    /**
     * Do a horizontal layout of the given components with the given spacing.
     *
     * @param c1 Component 1
     * @param c2 Component 2
     * @param c3 Component 3
     * @param spacing Inter component spacing
     * @return The new container of the components
     */
    public static JPanel hbox(Component c1, Component c2, Component c3, int spacing) {
        return hbox(new JPanel(), toList(new Object[] { c1, c2, c3 }), spacing);
    }

    /**
     * Do a horizontal layout of the given components.
     *
     * @param c1 Component 1
     * @param c2 Component 2
     * @return The new container of the components
     */

    public static JPanel hbox(Component c1, Component c2) {
        return hbox(toList(new Object[] { c1, c2 }));
    }

    /**
     * Do a horizontal layout of the given components.
     *
     * @param c1 Component 1
     * @param c2 Component 2
     * @param c3 Component 3
     * @return The new container of the components
     */

    public static JPanel hbox(Component c1, Component c2, Component c3) {
        return hbox(toList(new Object[] { c1, c2, c3 }));
    }

    /**
     * Do a horizontal layout of the given components.
     *
     * @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 hbox(Component c1, Component c2, Component c3, Component c4) {
        return hbox(toList(new Object[] { c1, c2, c3, c4 }));
    }

    /**
     * Do a horizontal layout of the given components.
     *
     * @param components The components
     * @param spacing The spacing
     * @return The new container of the components
     */
    public static JPanel hbox(List components, int spacing) {
        return hbox(new JPanel(), components, spacing);
    }

    /**
     * Do a horizontal layout of the given components.
     *
     * @param components The components
     * @return The new container of the components
     */
    public static JPanel hbox(List components) {
        return hbox(new JPanel(), components);
    }

    /**
     * Do a horizontal layout of the given components.
     *
     * @param components The components
     * @return The new container of the components
     */
    public static JPanel hbox(Component[] components) {
        return hbox(new JPanel(), toList(components));
    }

    /**
     * Do a horizontal layout of the given components.
     *
     * @param components The components
     * @param spacing _more_
     * @return The new container of the components
     */
    public static JPanel hbox(Component[] components, int spacing) {
        return hbox(new JPanel(), toList(components), spacing);
    }

    /**
     * Do a horizontal layout of the given components.
     *
     * @param panel The panel to use or, if null,  we'll create a new on.
     * @param components The components
     * @return The  container of the components
     */
    public static JPanel hbox(JPanel panel, List components) {
        return hbox(panel, components, 0);
    }

    /**
     * Do a horizontal layout of the given components.
     *
     * @param panel The panel to use or, if null,  we'll create a new on.
     * @param components The components
     * @param space SPacing to use
     * @return The  container of the components
     */
    public static JPanel hbox(JPanel panel, List components, int space) {
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        for (int i = 0; i < components.size(); i++) {
            panel.add((Component) components.get(i));
            if (space > 0) {
                panel.add(Box.createHorizontalStrut(space));
            }
        }
        return panel;
    }

    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. createVertBox(Component... comps)
  2. createVerticalBox(Component... comps)
  3. createVerticalBox(Component... cs)
  4. createVerticalBoxLayout(Component... components)
  5. execLoop(JComponent editor)
  6. makeVerticalBoxPanel(int margin)
  7. newBox(String name, String... items)
  8. newBoxForComponent(Component c)
  9. show(JComponent comp, String windowName)