left Align Box - Java Swing

Java examples for Swing:BoxLayout

Description

left Align Box

Demo Code


//package com.java2s;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JComponent;

public class Main {
    public static Component leftAlignBox(JComponent component) {
        Box b = Box.createHorizontalBox();
        b.add(Box.createHorizontalStrut(9));
        b.add(component);//from   w ww . j  av  a 2 s .  c  o m
        b.add(Box.createHorizontalGlue());
        setMaxHeightToPreferred(b);
        return b;
    }

    /**
     * Sets the maximum size of a {@link JComponent} to the preferred size
     * 
     * @param component
     *            The component to set the size of
     */
    public static void setMaxHeightToPreferred(JComponent component) {
        component.setMaximumSize(new Dimension(Integer.MAX_VALUE, component
                .getPreferredSize().height));
    }
}

Related Tutorials