Java tutorial
//package com.java2s; import java.awt.Dimension; import javax.swing.JComponent; public class Main { /** * This method encapsulates some ugly code. * It sets the preferred width of a component while keeping the preferred height intact. */ public static void setPreferredWidth(JComponent comp, int width) { Dimension preferredSize = comp.getPreferredSize(); preferredSize.width = width; comp.setPreferredSize(preferredSize); } }