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.
Please expand on what do you mean by "width of certain DOM elements"
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/
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/
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.
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.getFo nt());
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
if you are doing this one on Java GUI, you can try
label.setPreferredSize(new
//and
FontMetrics fm = new FontMetrics(myJLabel.getFo
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
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.
that is why I mentioned W3c DOM. and I mean the java lib, not javascript.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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?
ASKER
I said in the beginning that I am using w3c dom. nothing about xml.
here is some code
Element headerElement = document.createElement("la bel");
headerElement.setAttribute ("class", "x-form-item-label");
int lwidth = 12;
headerElement.setAttribute ("style", "width:"+lwidth+"px;paddin g: 0 0 0 0");
headerElement.setAttribute ("for", widgetIdString);
headerElement.setTextConte nt(header+ ":");
lwidth is what I need to calculate based on how many char in the label.
here is some code
Element headerElement = document.createElement("la
headerElement.setAttribute
int lwidth = 12;
headerElement.setAttribute
headerElement.setAttribute
headerElement.setTextConte
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();
String label = labelNode.getTextContent()
int lwidth = 12;
if( label != null ) lwidth = label.length();
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.