?
Solved

text metrics using w3c dom

Posted on 2009-12-29
15
Medium Priority
?
650 Views
Last Modified: 2013-12-29
what options are there to detect the width of certain DOM elements on the server. the DOM lib is the W3C lib.
0
Comment
Question by:bhomass
  • 7
  • 5
  • 3
15 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26143502
Please expand on what do you mean by "width of certain DOM elements"
0
 

Author Comment

by:bhomass
ID: 26148345
the element has a field label, the label text is not known until rendering time. need to know what label width to set based on how the text displays.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26151762
you can simply set the width dynamically based on the field text width
like
var fieldTextLength = fieldText.length;
width = fieldText * 2;

you can also adjust the width based on the font

following link should be helpful
http://www.alistapart.com/articles/fontresizing/
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:bhomass
ID: 26152250
I am missing something.

what is fieldText? do you mean the String fieldText? what multiply by 2? is that suppose to be the char length?

I thought there would be a TextMetrics class of some sort to do this.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26152927
ohh.ok..i thought that you want to do this one the HTML page

if you are doing this one on Java GUI, you can try
label.setPreferredSize(new Dimension(width,height));
//and
FontMetrics fm = new FontMetrics(myJLabel.getFont());
int height = fm.getHeight();

see this for reference
http://www.rhinocerus.net/forum/lang-java-gui/581862-layout-labels-variable-text-length.html

Let me know if i got it right this time
0
 

Author Comment

by:bhomass
ID: 26155597
we are still crossing thoughts. I am not using swing, this is for html page. however, I am processing the html on the server before sending response back to the browser.

that is why I mentioned W3c DOM. and I mean the java lib, not javascript.
0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 300 total points
ID: 26157823
Ok. If i have understood completely, then you are looking for APIs or Packages with which you can find/adjust the size of the field you are rendering later.

See the following link to understand on how can you do the formatting of text and dates using Java Text formatting package.
http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html
http://java.sun.com/docs/books/tutorial/2d/text/measuringtext.html

So, using these APIs you can measure the width of the text/field that you want to render on the screen.

Let me know if this was helpful
0
 

Author Comment

by:bhomass
ID: 26158861
I am NOT using SWING!
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 26181568
If you want help, then it would work best to post your code.

Are you using a JSP/Servlet page (you're in the JSP forum) or are you using XSLT and XML?

The problem is that your question doesn't seem to make sense.  If you have the content in your xml, then you read the xml and count the number of characters in the content of your label.  You generate your HTML page accordingly.  Is that what you mean?
0
 

Author Comment

by:bhomass
ID: 26183901
I said in the beginning that I am using w3c dom. nothing about xml.

here is some code

      Element headerElement = document.createElement("label");
      headerElement.setAttribute("class", "x-form-item-label");
      int lwidth = 12;
      headerElement.setAttribute("style", "width:"+lwidth+"px;padding: 0 0 0 0");            
      headerElement.setAttribute("for", widgetIdString);
      headerElement.setTextContent(header+":");

lwidth is what I need to calculate based on how many char in the label.

0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 26184944
Presumably you get label from somewhere, right?  So the code might look something like this:

String label = labelNode.getTextContent(); // you have to get the label
int lwidth = 12;
if( label != null ) lwidth = label.length();

0
 

Author Comment

by:bhomass
ID: 26185003
the length I need is in px, not in char.

label.getLength() tells me how many char. I need to know how many px that translates into.

I do have the info on what is the font used. I just need to find some TextMetric lib that can do the calculation for me.
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 26186459
Then I think you want the FontMetrics class.  Something like this to calculate pixels for the display width:

Font font = new Font("Verdana", Font.PLAIN, 10);
String label = "Whatever you want";
JLabel jLabel = new JLabel(label);
jLabel.setFont(font);
FontMetrics fm = jLabel.getFontMetrics(font);

int width = fm.stringWidth(jLabel.getText());

You'll need the java.awt.* classes and javax.swing.JLabel .
0
 

Author Comment

by:bhomass
ID: 26186939
I see, that worked!

so you have to use swing api for this. apologies to gurvinder. he was giving the right pointer, but to a lot of reading though.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question