Here you can find the source of setPreferredWidth(JComponent component, int width)
public static void setPreferredWidth(JComponent component, int width)
//package com.java2s; /*************************************************************************** This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License 3 as published by the Free Software Foundation./*www. j av a 2 s .c o m*/ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ***************************************************************************/ import java.awt.*; import javax.swing.*; public class Main { public static void setPreferredWidth(JComponent component, int width) { Dimension d = component.getPreferredSize(); d.width = width; component.setPreferredSize(d); } }