File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Number of characters per number of pixels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Number of characters per number of pixels" Watch "Number of characters per number of pixels" New topic
Author

Number of characters per number of pixels

Richard Everhart
Ranch Hand

Joined: Nov 19, 2003
Posts: 54
I need to determine how many characters in a given font can fit on in a java.awt.TextArea that is a known number of pixels wide. BTW, the TextArea's getColumn() method, in this case, will return zero.

I know I can easily determine the number of pixels a string will be in width with a given font, but I'm not sure how to go the other direction: from pixel count to char count vs. char count to pixel count.

Can anyone help?

Rich
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
You can only work out how many characters can by occupied in a certain number of pixels if you are using a monospaced font.

This is because an x and an i in a non-monospaced font take up a different number of pixels per character whereas a monospaced font takes up a defined number of pixels per character.

So for monospaced - find out the width of one character and then divide the pixels by that answer. (And for non-monospaced this won't work).

Cheers,
Rachel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number of characters per number of pixels