Calculate width of text.

I'm trying to find a way to calculate the width of a string of text in pixels. I know this will vary by font family and font size. Is there some built in way to do this? Has anyone done this before? Any help is greatly appreciated.
[239 byte] By [lokeloa] at [2007-10-3 3:23:41]
# 1

If you've got a Component handy, you could do

FontMetrics fm = c.getFontMetrics( c.getFont() );

int width = fm.stringWidth( s );

otherwise you could make use of a DebugGraphics

Graphics dg = DebugGraphics.create();

FontMetrics fm = dg.getFontMetrics( aFont );

int width = fm.stringWidth( s );

JayDSa at 2007-7-14 21:16:17 > top of Java-index,Desktop,Core GUI APIs...