Java Swing BoxLayout createVerticalBox(Component... comps)

Here you can find the source of createVerticalBox(Component... comps)

Description

create Vertical Box

License

Open Source License

Declaration

public static JPanel createVerticalBox(Component... comps) 

Method Source Code

//package com.java2s;
/*/*  w  w w.ja v  a 2 s  . c  om*/
 * Spirit, a study/biosample management tool for research.
 * Copyright (C) 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91,
 * CH-4123 Allschwil, Switzerland.
 *
 * 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/>
 *
 * @author Joel Freyss
 */

import java.awt.Component;

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

import java.util.List;

import javax.swing.Box.Filler;

import javax.swing.JPanel;

import javax.swing.border.Border;

public class Main {
    public static JPanel createVerticalBox(Border border, Component... comps) {
        JPanel res = createVerticalBox(comps);
        res.setBorder(border);
        return res;
    }

    public static JPanel createVerticalBox(Border border, List<? extends Component> comps) {
        JPanel res = createVerticalBox(comps);
        res.setBorder(border);
        return res;
    }

    public static JPanel createVerticalBox(List<? extends Component> comps) {
        return createVerticalBox(comps.toArray(new Component[0]));
    }

    public static JPanel createVerticalBox(Component... comps) {
        return createVerticalBox((int[]) null, comps);
    }

    public static JPanel createVerticalBox(int[] ratios, Component... comps) {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = GridBagConstraints.RELATIVE;
        c.anchor = GridBagConstraints.WEST;
        c.fill = GridBagConstraints.BOTH;
        for (int i = 0; i < comps.length; i++) {
            Component comp = comps[i];
            if (comp == null) {
                continue;
            } else if (comp instanceof Filler && ((Filler) comp).getMinimumSize().height == 0) {
                c.weightx = c.weighty = 1;
            } else {
                c.weightx = 1;
                c.weighty = ratios == null || i >= comps.length ? 0.01 : ratios[i];
            }
            panel.add(comp, c);
        }
        panel.setOpaque(false);

        return panel;
    }
}

Related

  1. createPanelBoxLayout(Component... components)
  2. createPanelWithBoxLayout()
  3. createTitledPanel(JComponent component, String title)
  4. createTopAndCenter(JComponent top, JComponent center)
  5. createVertBox(Component... comps)
  6. createVerticalBox(Component... cs)
  7. createVerticalBoxLayout(Component... components)
  8. execLoop(JComponent editor)
  9. hbox(Component[] components, int spacing)