Solved

Columns To Pixel Conversion Help Needed

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

708 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

15 Experts available now in Live!

Get 1:1 Help Now