Calculate String Width

Is it possible to calculate the width of a String in a certain font without needing a Graphics object? I need to calculate this before a component is displayed in order to adjust the sizes of the components. getGraphics() is returning null at this point.
[261 byte] By [coryb891a] at [2007-11-26 15:53:03]
# 1
I think the [url= http://java.sun.com/j2se/1.5.0/docs/api/java/awt/FontMetrics.html]FontMetrics[/url] class can do this.
CaptainMorgan08a at 2007-7-8 22:13:28 > top of Java-index,Desktop,Core GUI APIs...
# 2
It is possible with the Font and FontMetrics classes available in java.awt package. Refer to the API for furthur information. If possible I will send u one example soon.Bye...
santhosh-mcaa at 2007-7-8 22:13:28 > top of Java-index,Desktop,Core GUI APIs...
# 3
I was actually looking at using this yesterday but I couldn't find any way to get a FontMetrics instance without going through the Graphics class (Graphics.getFontMetrics()). Similarly, I can't get a FontRenderContext without a Graphics instance.
coryb891a at 2007-7-8 22:13:28 > top of Java-index,Desktop,Core GUI APIs...
# 4
> I couldn't find any way to get a FontMetrics instance> without going through the Graphics classWhy would you need to calculate the dimensions of a String without a Graphics object?
CaptainMorgan08a at 2007-7-8 22:13:28 > top of Java-index,Desktop,Core GUI APIs...
# 5

Didn't my original post answer that question?

My control is being wrapped in a JScrollPane. I need to set the preferred size of the scroll pane based on the width of a String. If I try to do that during initialization before it is displayed, getGraphics() returns null so I get a NullPointerException.

coryb891a at 2007-7-8 22:13:28 > top of Java-index,Desktop,Core GUI APIs...