?
Solved

Columns To Pixel Conversion Help Needed

Posted on 1997-06-06
4
Medium Priority
?
869 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 400 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

765 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