Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Dimension; public class Main { /** Sets preferred width of the component, leaving the height intact. */ public static void setPreferredWidth(Component component, int width) { Dimension dim = component.getPreferredSize(); component.setPreferredSize(new Dimension(width, dim.height)); } }