set JComponent Shrinkable - Java Swing

Java examples for Swing:JComponent

Description

set JComponent Shrinkable

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 setShrinkable(final JComponent aComponent,
            final boolean aHorizontal, final boolean aVertical) {

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

                Dimension preferred = aComponent.getPreferredSize();
                aComponent
                        .setMinimumSize(new Dimension(aHorizontal ? 0
                                : preferred.width, aVertical ? 0
                                : preferred.height));

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

Related Tutorials