set JComponent Expandable - Java Swing

Java examples for Swing:JComponent

Description

set JComponent Expandable

Demo Code


import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class Main{
    public static void setExpandable(final JComponent aComponent,
            final boolean aHorizontal, final boolean aVertical) {

        aComponent.addAncestorListener(new AncestorAdapter() {
            @Override//from   w ww  . j a va  2s .c o m
            public void ancestorAdded(AncestorEvent aEvent) {

                Dimension preferred = aComponent.getPreferredSize();
                aComponent.setMaximumSize(new Dimension(
                        aHorizontal ? Integer.MAX_VALUE : preferred.width,
                        aVertical ? Integer.MAX_VALUE : preferred.height));

                aComponent.removeAncestorListener(this);
            }
        });
    }
}

Related Tutorials