This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes does java have a class to find string width? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "does java have a class to find string width?" Watch "does java have a class to find string width?" New topic
Author

does java have a class to find string width?

Tina Long
Ranch Hand

Joined: Mar 04, 2005
Posts: 36
Does anyone have any suggestions on what I could do to figure out the width of a string?

Ex:
If I have two strings:
"WHOOO HOOO"
vs
"ITTY BITTY"

Both strings have the same number of letters however when I use a font that is not fixed width - then the second string takes up less room when typed. Is there anything out there that will help me find the width of the string?

I need to take in account the font used and the string written - I've been looking at the Font class - but not finding what I need. Thoughts anyone?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

FontMetrics.


[Jess in Action][AskingGoodQuestions]
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Look at the FontMetrics class.


Regards,<br />Seb<br /> <br />SCJP 1.4
Tina Long
Ranch Hand

Joined: Mar 04, 2005
Posts: 36
Thanks - I believe that will work for me! YAY!

How did you know that? Is it experience? I searched and read and I finally gave up and asked. Thanks for your quick response.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Experience. FontMetrics is one of the original few hundred API classes, from way back in the Java 1.0 days when it was possible to know about all the classes in the API.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: does java have a class to find string width?
 
Similar Threads
Wanted: Java2D example of zooming and panning with scroll bars
calculate string width
Creating a image file using TextLayout. Need to find right startYpoint
String's intern() - details
Centering text on object drawn with Graphics