Here you can find the source of computeStringWidth(FontMetrics fontMetrics, String str)
public static int computeStringWidth(FontMetrics fontMetrics, String str)
//package com.java2s; /* //from w w w . ja v a 2 s . com * @(#)StringUtil.java 1.0 2004-10-11 * * Copyright 2005 UFIDA Software Co. Ltd. All rights reserved. * UFIDA PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ import java.awt.FontMetrics; public class Main { public static int computeStringWidth(FontMetrics fontMetrics, String str) { if (str == null || str.length() <= 0) return 0; int width = 10 + javax.swing.SwingUtilities.computeStringWidth(fontMetrics, str); int bytesLen = str.getBytes().length; if (bytesLen >= 10) width += (bytesLen - 10) * 2 + 5; return width; } }