Freezes the width of the given component, preventing it from expanding to fill any additional space. - Java Swing

Java examples for Swing:JComponent

Description

Freezes the width of the given component, preventing it from expanding to fill any additional space.

Demo Code


//package com.java2s;

import java.awt.Dimension;

import javax.swing.JComponent;

public class Main {
    /**/*from  w  w  w  . j a  v a 2  s  .c o  m*/
     * Freezes the width of the given component, preventing it from expanding
     * to fill any additional space.
     * @param component the component to freeze
     * @return the component passed in, for chaining purposes
     */
    public static JComponent freezeWidth(JComponent component) {
        return freezeWidth(component, component.getPreferredSize().width);
    }

    /**
     * Freezes the width of the given component to the given size, preventing
     * it from expanding to fill any additional space.
     * @param component the component to freeze
     * @param width the width to freeze the component to
     * @return the component passed in, for chaining purposes
     */
    public static JComponent freezeWidth(JComponent component, int width) {
        component.setMinimumSize(new Dimension(width, component
                .getMinimumSize().height));
        component.setPreferredSize(new Dimension(width, component
                .getPreferredSize().height));
        component.setMaximumSize(new Dimension(width, component
                .getMaximumSize().height));
        return component;
    }
}

Related Tutorials