Return new JPanel with the specified components laid out in a BoxLayout along the Y-axis - Java Swing

Java examples for Swing:Layout Manager

Description

Return new JPanel with the specified components laid out in a BoxLayout along the Y-axis

Demo Code


//package com.java2s;
import java.awt.Component;

import java.awt.LayoutManager;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

public class Main {
    /**//from w w w . ja  v a2s. com
     * Return new JPanel with the specified components laid out in a BoxLayout along the Y-axis
     * @param components components to place in JPanel
     * @return new JPanel
     */
    public static JPanel yBoxLayoutPanel(Component... components) {
        return newJPanel(BoxLayout.Y_AXIS, components);
    }

    private static JPanel newJPanel(int layoutAxis, Component... components) {
        JPanel retval = new JPanel();
        retval.setLayout(new BoxLayout(retval, layoutAxis));
        for (Component component : components)
            retval.add(component);
        return retval;
    }

    /**
     * Return new JPanel with specified LayoutManager, containing specified components
     * @param layoutManager layout manager to use
     * @param components components to place into panel
     * @return new JPanel with specified LayoutManager, containing specified components
     */
    public static JPanel newJPanel(LayoutManager layoutManager,
            Component... components) {
        JPanel retval = new JPanel();
        retval.setLayout(layoutManager);
        for (Component component : components)
            retval.add(component);
        return retval;
    }
}

Related Tutorials