• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pixel width of a String

 
Jon Christensen
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jon Christensen
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

An other method is to use the
SwingUtilities method:


Olek
 
Hieu Nguyen D.
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Tomasz and Tim, it really helped me. I'd like to wake zombies to say thanks.
 
jordan wolfe
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic