aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Pixel width of a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Pixel width of a String" Watch "Pixel width of a String" New topic
Author

Pixel width of a String

Jon Christensen
Greenhorn

Joined: Apr 14, 2003
Posts: 21
Is there anyway to determine the pixel width of a given string? We need to set a length for a javascript menu in pixels dynamically based on the length of the string. The string could be in a number of different languages so we need to dynamically get the pixel width.

Any ideas?

Thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
If Java was doing the display you could use FontMetrics, but since you appear to be talking about the browser display, I don't think you can compute exact pixels on the Java side.
You should investigate Cascading Style Sheets to control the layout rather than trying for exact pixel calculation.
Bill
Jon Christensen
Greenhorn

Joined: Apr 14, 2003
Posts: 21
Unfortunately, the menu code that we have needs a pixel width and height to display the menu correctly.

We have tried a formula to compute the max pixel width that a string might need but it doesn't seem to be working. Somehow we came up with a max pixel width of a character would be 13 so we multiply the string length by 13 to get the pixel width. This would be good if the only letter they used was 'W'.
[ January 20, 2005: Message edited by: Jon Christensen ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
JavaScript is NOT the same as Java. You should ask your question at the HTML and JavaScript forum. You will probably get a more informative answer there.

Good Luck!

Layne


Java API Documentation
The Java Tutorial
Jon Christensen
Greenhorn

Joined: Apr 14, 2003
Posts: 21
Yes, I know they are not the same. The computation of the length of the string is done in JAVA.

The javascript is there to create the menu based on the value that is used in the JSP.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'm sorry. I must have misunderstood your post. I didn't see anywhere that it mentioned Java.

Anyway, as mentioned above, you may be able to use the FontMetrics class. However, this is from the java.awt package so I'm not sure if it will work correctly outside of an AWT or applet GUI. It's probably worth a try, though.

Layne
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
It would probably help if you can use CSS or the HTML FONT tag to force the font used in the HTML menu to be monospaced (Courier?) and of a specified font size. But that would only work for ASCII.
Trying to figure out variable width fonts is a real pain.
Perhaps you can use some Javascript / CSS trick to write some sample text in the desired language, and get Javascript to find out how much space it takes in the current browser settings, then send this information back to the server.
For this kind of experimentation, I really LOVE the FireFox browser because you can use the DOM inspector tool to look at the real HTML DOM. Note that it helps alot if you use CSS to specify styles.
As Layne said - the HTML and Javascript forum is where you can get expert help on this.
Bill
Tomasz Lipinski
Greenhorn

Joined: Nov 05, 2008
Posts: 6
Originally posted by Layne Lund:
I'm sorry. I must have misunderstood your post. I didn't see anywhere that it mentioned Java.

Anyway, as mentioned above, you may be able to use the FontMetrics class. However, this is from the java.awt package so I'm not sure if it will work correctly outside of an AWT or applet GUI. It's probably worth a try, though.


I know that you asked long time ago, but it might be helpful for someone. It is possible to use FontMetrics outside AWT or Swing, try something like this:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Although we appreciate your input, please Don't Wake The Zombies. This thread is almost 4 years old, and both Jon and Layne have not been active for the last 2 years.

Also, please read your private messages regarding an important announcement.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pixel width of a String