Solved

Columns To Pixel Conversion Help Needed

Posted on 1997-06-06
4
845 Views
Last Modified: 2012-06-27
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)).
0
Comment
Question by:dbarstis060697
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 100 total points
ID: 1221310
The colimn to pixel ratio is variable and depends on the font you are using.

The problem is further complicated by the fact that not all fonts are fixed width. Unfortunatley, Java has no way to determine what type of font is being used.

What you have to do then is to set up an Instance of FontMetrics and get the width of the string thatt is currently selected.

FontMetrics fm_arial = new FontMetrics("arial");
w_arial = fm.sstringWidth(yourList.getSelectedItem());
w_arial /= yourList.getSelectedItem().length();

With this, w_arial will contain the average width of a an arial character (which happens to be fixed length). This algorithm will work fine for short strings, but will fail for long strings that are not fixed width due to cumulative offset errors.

For non fixed width fonts you would have to add the width of each character in the sting. This would still produce a small offset error, but the error would be at the character level, rather than the string level and would be non cumulative. I.e: you might encounter characters that do not exactly begin or end where you expected them to after the calculation.

This last aspect reaps havoc for DTP applications, so companies like Correl had to manage all their work in graphics rather than taking advantage of many of AWT's features.

Regarding height, use FontMetrics.getHeight().

Once you have values for width and height in pixels, you can do any convertions you desire.

0
 

Author Comment

by:dbarstis060697
ID: 1221311
The problem is that I don't have a string to select yet.
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);
}

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1221312
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.getAverageWidth() 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.
0
 

Author Comment

by:dbarstis060697
ID: 1221313
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question