This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
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: 12759
    
    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: 12759
    
    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:

Tim Langhammer
Greenhorn

Joined: Sep 23, 2009
Posts: 1
Hi,

An other method is to use the
SwingUtilities method:


Olek
Hieu Nguyen D.
Greenhorn

Joined: Jan 04, 2010
Posts: 2
Thank Tomasz and Tim, it really helped me. I'd like to wake zombies to say thanks.
jordan wolfe
Greenhorn

Joined: Mar 17, 2010
Posts: 1
Just wanted to say thanks - this totally helped me out.

Also good to see that waking the zombies is now encouraged - I've always favored the approach that gets more solutions out there
Aldoria Robichaud
Greenhorn

Joined: Jun 01, 2010
Posts: 5
I hope I don't get flamed for 'waking the zombies' or whatever, but its more efficient to post here than on a new thread.

For the method:
SwingUtilities.computeStringWidth( fontMetrics, stringToComputeTheWidth );

Does anyone have an example code of this being used? I'm not familiar with fontMetrics at all. Do I need to declare it somewhere?


I'm not the brightest light bulb. I'm not the sharpest tool in the drawer. I'm not the smartest student in the classroom. I couldn't cut the pickle. But hey, at least I try, right? Or is that a bad thing..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pixel width of a String
 
Similar Threads
Pixel width of a String
EPL2 - Load image into to Zebra printer
How to get a String width(pixel or Metric)
JTable column width
Basic question about constructors