Here you can find the source of setPreferredWidth(JComponent component, int width)
public static void setPreferredWidth(JComponent component, int width)
//package com.java2s; //License from project: Apache License import java.awt.Dimension; import javax.swing.JComponent; public class Main { public static void setPreferredWidth(JComponent component, int width) { component.setPreferredSize(getPreferredSize(component, width)); }//from w w w.j a v a 2 s . c o m /** * Returns the preferred dimensions of a component, using the current height * and a custom width. * @param component The component providing the preferred height. * @param width The preferred width. * @return The preferred dimension. */ public static Dimension getPreferredSize(JComponent component, int width) { Dimension dim = component.getPreferredSize(); return new Dimension(width, dim.height); } }