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.
bhomassAsked:
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);
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
 
Gurvinder Pal SinghCommented:
Please expand on what do you mean by "width of certain DOM elements"
0
 
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.
0
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
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
 
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.
0
 
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));
//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
 
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.
0
 
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.
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
 
bhomassAuthor Commented:
I am NOT using SWING!
0
 
mrcoffee365Commented:
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
 
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);
      headerElement.setTextContent(header+":");

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

0
 
mrcoffee365Commented:
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
 
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.
0
 
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.
0
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.