File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 24043
    
  13

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: 24043
    
  13

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://zeroturnaround.com/jrebel/download
 
subject: does java have a class to find string width?
 
Similar Threads
Creating a image file using TextLayout. Need to find right startYpoint
Centering text on object drawn with Graphics
calculate string width
Wanted: Java2D example of zooming and panning with scroll bars
String's intern() - details