How to calculate width of a String
in pixels in Java? For e.g., I have a string say "Hello World!". What is its length in pixels, also considering its font family and size?

- 109,525
- 20
- 134
- 319

- 884
- 8
- 15
-
Well, do you have a special font and font size in mind? – Matthias Aug 20 '13 at 05:52
-
It can be arbitrarily anyone. – user2550754 Aug 20 '13 at 05:53
-
Edited your formatting to remove the code markup for length in particular. – s.bandara Aug 20 '13 at 05:54
-
1Start by taking a look at [Drawing Mutliple Lines of Text](http://docs.oracle.com/javase/tutorial/2d/text/drawmulstring.html) and [`FontMetrics#stringWidth`](http://docs.oracle.com/javase/7/docs/api/java/awt/FontMetrics.html) – MadProgrammer Aug 20 '13 at 05:55
-
Are you asking anything different from [this](http://stackoverflow.com/questions/258486/calculate-the-display-width-of-a-string-in-java)? – devnull Aug 20 '13 at 05:59
-
[This](http://stackoverflow.com/questions/13345712/string-length-in-pixels-in-java) might also answer your question. – devnull Aug 20 '13 at 06:01
5 Answers
In short: the question depends on the framework you are using. For example, if you are using AWT, and have a Graphics
object graphics
, and a Font
object font
you can do the following:
FontMetrics metrics = graphics.getFontMetrics(font);
int width = metrics.stringWidth("Hello world!");
Check out this for more information.

- 1,136
- 1
- 6
- 15
You can try something like this
Graphics2D g2d = ...
Font font = ...
Rectangle2D r = font.getStringBounds("hello world!", g2d.getFontRenderContext());
System.out.println("(" + r.getWidth() + ", " + r.getHeight() + ")");
Refer this doc, may help you.

- 34,993
- 17
- 75
- 115
-
It seems a little convaluted considering you can use `Graphics#getFontMetrics`, but I'll give you an up vote for being different ;) – MadProgrammer Aug 20 '13 at 06:05
There are a number of ways to achieve what you want, based on what it is you want to achieve, for example...
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
FontMetrics fm = g2d.getFontMetrics();
System.out.println(fm.stringWidth("This is a simple test"));
g2d.dispose();
But this only has relevence for the BufferedImage
and it's Graphics
context, it will not translate back to say, something like a screen or printer.
However, so long as you have a Graphics
context, you can achieve the same result.
This example, obviously, uses the default font installed for the Graphics
context, which you can change if you need to...

- 343,457
- 22
- 230
- 366
-
I just have a `String`. I know its length, font family, font size. I want to calculate its size in pixels. – user2550754 Aug 20 '13 at 06:02
-
Fine, then you can use something like the above. The width and height is depended on the `Graphics` context you are rendering to. While you have the font family, font size and text, you are missing the key element – MadProgrammer Aug 20 '13 at 06:03
there are couple of ways to do it, you can try label.getElement().getClientWidth();
if the text is in a lable, if you're using AWT you can use Graphics.getFontMetrics
then FontMetrics.stringWidth

- 1,240
- 2
- 11
- 21
You can use FontMetrics. The FontMetrics class defines a font metrics object, which encapsulates information about the rendering of a particular font on a particular screen.
You can do something like below
Font font = new Font("Verdana", Font.PLAIN, 10);
FontMetrics metrics = new FontMetrics(font) {
};
Rectangle2D bounds = metrics.getStringBounds("Hello World!", null);
int widthInPixels = (int) bounds.getWidth();

- 66,731
- 38
- 279
- 289
-
I've got a pretty good feeling that this isn't going to work. The pixel size will be depended on the `Graphics` context, which you're passing `null` for... – MadProgrammer Aug 20 '13 at 06:06
-
Not sure how to cross verify the result but when I ran it I got 67 as the output for the string Hello World!. – Aniket Thakur Aug 20 '13 at 06:08