Java tutorial
//package com.java2s; import java.awt.Dimension; import javax.swing.JComponent; public class Main { /** * 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; } }