Java tutorial
//package com.java2s; import java.awt.Dimension; import java.awt.FontMetrics; import javax.swing.JComponent; import sun.swing.SwingUtilities2; public class Main { public static Dimension getStringSize(JComponent c, FontMetrics metrics, String text) { int width = SwingUtilities2.stringWidth(c, metrics, text); int height = metrics.getHeight(); return new Dimension(width, height); } }