Link to home
Start Free TrialLog in
Avatar of bhomass
bhomass

asked on

text metrics using w3c dom

what options are there to detect the width of certain DOM elements on the server. the DOM lib is the W3C lib.
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Please expand on what do you mean by "width of certain DOM elements"
Avatar of bhomass
bhomass

ASKER

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.
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/
Avatar of bhomass

ASKER

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.
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
Avatar of bhomass

ASKER

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.
SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bhomass

ASKER

I am NOT using SWING!
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?
Avatar of bhomass

ASKER

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.

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();

Avatar of bhomass

ASKER

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bhomass

ASKER

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.