Solved

Columns To Pixel Conversion Help Needed

Posted on 1997-06-06
4
826 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

914 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now