This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes How to get a String width(pixel or Metric) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to get a String width(pixel or Metric)" Watch "How to get a String width(pixel or Metric)" New topic
Author

How to get a String width(pixel or Metric)

daniel xiao
Greenhorn

Joined: Jul 04, 2003
Posts: 12
How to get a String width(pixel or Metric)??
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

FontMetrics.stringWidth( String ) returns an int width (I think this is in pixels? )

You can get the FontMetrics object through a Graphics object's getFontMetrics() or getFontMetrics( Font ) methods.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to get a String width(pixel or Metric)