I have to take the text from a JTextField and find out what the size of the String is in pixels.
I see that from Graphics I could get FontMetrics and that would be the solution, but in this instance, I can't get a handle to the graphics object as it is not yet displayed. I've also looked at Toolkit.getFontMetrics but that gives a whole long winded thing about being Deprecated.
Here's a demo that explores font properties. I put everything inside the paintComponent method for the demo but you can just as easily do the computations outside, ie, without the graphics context — check out the three–argument constructor in the FontRenderContext api. You can use null for the AffineTransform argument.
Also check the Font getStringBounds and LineMetrics getHeight methods for information on what is included in the height. I left out the leading in the demo. You can experiment with the height factors to get the results you need; endless fun...
posted 14 years ago
Thanks a bunch, I haven't had a play with it yet, but it looks to be just what I need!
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
how do I do my own kindle-like thing - without amazon