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.
Who is Participating?
mrcoffee365Connect With a Mentor Commented:
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);
FontMetrics fm = jLabel.getFontMetrics(font);

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

You'll need the java.awt.* classes and javax.swing.JLabel .
Gurvinder Pal SinghCommented:
Please expand on what do you mean by "width of certain DOM elements"
bhomassAuthor Commented:
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.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Gurvinder Pal SinghCommented:
you can simply set the width dynamically based on the field text width
var fieldTextLength = fieldText.length;
width = fieldText * 2;

you can also adjust the width based on the font

following link should be helpful
bhomassAuthor Commented:
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.
Gurvinder Pal SinghCommented:
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));
FontMetrics fm = new FontMetrics(myJLabel.getFont());
int height = fm.getHeight();

see this for reference

Let me know if i got it right this time
bhomassAuthor Commented:
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.
Gurvinder Pal SinghConnect With a Mentor Commented:
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.

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
bhomassAuthor Commented:
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?
bhomassAuthor Commented:
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);

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

bhomassAuthor Commented:
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.
bhomassAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.