Here you can find the source of getMaxWidth(final JComboBox
public static int getMaxWidth(final JComboBox<String> combo, final FontMetrics fm)
//package com.java2s; import java.awt.FontMetrics; import javax.swing.JComboBox; public class Main { public static int getMaxWidth(final JComboBox<String> combo, final FontMetrics fm) { int maxLen = 0; for (int i = 0; i < combo.getItemCount(); i++) { String str = combo.getItemAt(i); int wi = fm.stringWidth(str); if (wi > maxLen) { maxLen = wi;// w ww. j a v a2 s . c o m } } return maxLen + 20; // leave room for the scroll bar } }