dbarstis060697
asked on
Columns To Pixel Conversion Help Needed
Does anyone know how the column field from a TextArea is converted to a pixel width when given row and column parameters (i.e. TextArea(rows,cols))?
I want to extend the List class and include a column parameter. Everything is working except the width and height of Dimension are in pixels. Rows are
already converted (List(rows, multipleSelections)).
I want to extend the List class and include a column parameter. Everything is working except the width and height of Dimension are in pixels. Rows are
already converted (List(rows, multipleSelections)).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That will work for fixed width fonts, for variable width though I would sugest you use the string "isGW"(or the sum of the character widths in the string) and obtain the average width. This string will provide a fairly close to true average length.
On a variable width font, you can fit 4 or 5 i's in the space ocupied by 1 W.
Another way you can go about this is to predetermine the maximum size of a textField (I can provide code for a subclassed textfield in which you can specify the max. input length). You could then provide a sample string for width measurment of the exact length you wish to operate with.
One final option, albeit ugly, is tu use a native method to call your windows (or XWindow's) environment and call its getFontMetrics.getAverageW idth() method. Or you could write a small C program and run it to produce a table of these values for the fonts available under Java.
On a variable width font, you can fit 4 or 5 i's in the space ocupied by 1 W.
Another way you can go about this is to predetermine the maximum size of a textField (I can provide code for a subclassed textfield in which you can specify the max. input length). You could then provide a sample string for width measurment of the exact length you wish to operate with.
One final option, albeit ugly, is tu use a native method to call your windows (or XWindow's) environment and call its getFontMetrics.getAverageW
ASKER
Thanks for the info. All I wanted was the convenience of specifying a column width similar to a textArea. I have a list
and textarea on the same frame and wanted them to act the same.
If it's done to a textarea, one should be able to do it to a list
the same way.
and textarea on the same frame and wanted them to act the same.
If it's done to a textarea, one should be able to do it to a list
the same way.
ASKER
By setting the font, I can guarantee a fixed length. Then
what about using charWidth? i.e.
public Dimension preferredSize () {
FontMetrics fm=getFontMetrics(getFont(
// the character W used as reference
return new Dimension
(fm.charWidth('W')*width, fm.getHeight()*height);
}