Solved

Columns To Pixel Conversion Help Needed

Posted on 1997-06-06
4
861 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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 …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

688 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