Adds a HorizontalBox containing a "line" of JComponents to the indicated parent container. - Java Swing

Java examples for Swing:BoxLayout

Description

Adds a HorizontalBox containing a "line" of JComponents to the indicated parent container.

Demo Code


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

import javax.swing.Box;

import javax.swing.JComponent;

public class Main {
    /** /*  w w w.j  a  v  a 2s .c  o m*/
     *  Adds a HorizontalBox containing a "line" of JComponents to the indicated
     *  parent container.
     *  @param parent the JComponent to add the row to
     *  @param items the JComponents to add to parent
     *  @param maxSize the maximum size of an item in the row
     *  @param gap the gap between rows and items in a row
     */
    public static void addBoxRow(JComponent parent, JComponent[] items,
            Dimension maxSize, Dimension gap) {
        Box row = Box.createHorizontalBox();

        for (int i = 0; i < items.length; i++) {
            items[i].setMaximumSize(maxSize);
            row.add(items[i]);
            if (i != items.length - 1) {
                row.add(Box.createHorizontalGlue());
                row.add(Box.createHorizontalStrut((int) gap.getWidth()));
            }
        }

        parent.add(row);
        parent.add(Box.createVerticalStrut((int) gap.getHeight()));
    }
}

Related Tutorials