That does not tell you the height of the text area. That tells you the width.
Main Topics
Browse All TopicsI would like to create a dialog that displays text that will be loaded from an external source. I do not know exactly what the text will be, but under normal circumstances it will be able to fit comfortably in the dialog.
Assuming the text is not too long, I would like it to appear wrapped in the dialog without scroll bars. But I do not want extra white space if the text is short.
In order to accomplish this, I think I need a method that will determine the height of a JTextArea or JTextPane when wrapped to a specified width. But I have not found any such method.
Is anyone aware of such a method, or of any other way to accomplish this?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
From the documentation on FontMetrics.getHeight():
public int getHeight()
Determines the standard height of a line of text in the font described by this font metric. This standard height is the distance between the baseline of adjacent lines of text. It is computed as the sum
getLeading() + getAscent() + getDescent()
There is no guarantee that lines of text spaced at this distance must be disjoint; such lines may overlap if some characters overshoot either the standard ascent or the standard descent.
Returns: the standard height of the font.
Sasha.
You can inspire from the following example.
Cheers,
Bertrand
--------------------------
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class MyDialog extends JDialog {
private JTextArea area = null;
public MyDialog(Frame frame) {
super(frame, "Test", false);
// create the text area, and make it wrap lines
area = new JTextArea();
area.setLineWrap(true);
area.setWrapStyleWord(true
// add the text area to the dialog
getContentPane().add(area)
}
public void setText(String text) {
// display the text in the JTextArea
area.setText(text);
// set the size of the area to the appropriate
// width and an arbitrary height
area.setSize(new Dimension(getSize().width,
// now, pack the dialog
pack();
return;
}
public static void main(String args[]) {
MyDialog myDialog = new MyDialog(new JFrame());
myDialog.setSize(new Dimension(50,Integer.MAX_V
myDialog.setText("Replace this text with the appropriate one!");
myDialog.setVisible(true);
return;
}
}
I do not think you really read the question, Sasha. The various FontMetrics methods such as getHeight() return the metrics of a single line of text, not the height of a JTextArea or JTextPane. The text area will likely contain multiple lines of text, and without running through the line wrapping algorithms that the text component performs, you do not know how many lines. Then there are the borders and margins that can contribute to the height of the component.
Fontaine's response looks promising. I will check it out and post the results.
In general you can't control the size of your components by yourself. This is done by means of a LayoutManager. Even query the size of your JTextArea will be difficult because this size is set by the LayoutManager and may be valid only about the time your dialog is made visible.
So what I suggest is to set our text in your JTextArea, wrap this JTextArea in a JScrollPane and put it on the JDialog using a proper LayoutManager.
If the size of the JTextPane is large enough, no scroll bars will be drawed. The preferred size of your JTextArea will depend on the text and fonts choosen. If you choose a LayoutManager dealing with preferred sizes (as FlowLayout) the size of your JTextArea will not larger than needed.
In general the size of your dialog will depend on the LayoutManager, but it will not be larger than needed. You can setup a maximum size. If now there is too much text, scrollbars will appear !
Dear stelet,
May I friendly lead your attention to an important issue? Here in Experts-Exchange it is common practice to lock questions with an answer only if you are the first expert in the thread, or if all other comments are
evidently wrong. Otherwise it is usual to post a *comment* only. This way the questioner can later easily choose which expert helped most. Additionally, using comments only is more polite because:
- the question can easily be deleted if necessary
- it cannot happen that an answer is accidentally accepted
- you show that you acknowledge the work already done
- you give other experts a chance to post a better answer
Please, be so kind and use the option "withdraw answer" to convert your answer to a normal comment.
Thank you for listening... (-:
Sasha.
First, read Sasha's comments.
Second, your comments about not being able to control the size of a component are not accurate. You can always set the layout to null, and then size things as you would like. Or better yet, just write a custom LayoutManager, since this will allow client code to modify this behavior if they so wish.
Third, your "solution" does not address the problem. Rather it explains the basics of layout managers, which is pretty obvious.
Finally, your answer says that I can't do what I want to do, after others have posted suggestions on how to do it. If you had addressed their solutions, and said why they will not work, that may have been reasonable, but you did not.
Well, fontaine's answer is on the right track, but something is not quite right.
Here is the version of his code that I am working with. It is pretty much the same, except:
1) I made the dialog modal so I can exit after the dialog is closed. No zombie processes that way.
2) I changed the text for the text pane.
3) I added a setLocation() so the dialog does not open up under my task bar.
4) I added a show() method to dump out some information at different spots.
None of these changes should have had an effect on the sizing of the text area. But what is interesting is that the debug code makes it work.
Here is the program:
==========================
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class MyDialog extends JDialog {
public JTextArea area = null;
public MyDialog(Frame frame) {
super( frame, "Test", true );
// create the text area, and make it wrap lines
area = new JTextArea();
area.setLineWrap(true);
area.setWrapStyleWord(true
// add the text area to the dialog
getContentPane().add(area)
}
public void setText(String text) {
// display the text in the JTextArea
area.setText(text);
show( this, "After area.setText()" );
// set the size of the area to the appropriate
// width and an arbitrary height
area.setSize(new Dimension(getSize().width,
show( this, "After area.setSize()" );
// now, pack the dialog
pack();
show( this, "After pack()" );
return;
}
public static void main(String args[]) {
MyDialog myDialog = new MyDialog(new JFrame());
show( myDialog, "After constructor" );
myDialog.setSize( new Dimension( 400, Integer.MAX_VALUE ) );
show( myDialog, "After setSize()" );
myDialog.setText(
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit.\n\n" +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. "
);
myDialog.setLocation( 200, 200 );
myDialog.setVisible(true);
System.exit( 0 );
}
static void show( MyDialog myDialog, String desc ) {
/**/
System.out.println( "\n" + desc + ":" );
System.out.println( " size = " + myDialog.getSize() );
System.out.println( " preferredSize = " + myDialog.getPreferredSize(
System.out.println( " rows = " + myDialog.area.getRows() );
System.out.println( " columns = " + myDialog.area.getColumns()
/**/
}
}
==========================
If you comment out the body of the show() method (change "/**/" to "/** /"), the program does not work.
Curiouser and curiouser.
I will let you know what I find out.
Here is a restructured program:
==========================
import javax.swing.*;
public class TextViewer
extends JTextArea
{
public TextViewer() {
setLineWrap( true );
setWrapStyleWord( true );
setColumns( 35 );
setSize( getPreferredSize().width, Integer.MAX_VALUE );
// Without this, it does not work.
getPreferredSize();
}
public static void main(String args[]) {
JDialog dialog = new JDialog( new JFrame(), "Text Viewer", true );
TextViewer textViewer = new TextViewer();
textViewer.setText(
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit.\n\n" +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. " +
"This is a test of sizing a text area to fit. "
);
dialog.getContentPane().ad
dialog.pack();
dialog.setLocation( 200, 200 );
dialog.setVisible( true );
System.exit( 0 );
}
}
==========================
Any idea what is going on? In particular, is this behavior a fluke, or is this supposed to work this way?
Business Accounts
Answer for Membership
by: Sasha_MapaPosted on 2000-08-27 at 05:36:14ID: 4087028
Use the java.awt.FontMetrics class to determine the size of the string at a given font...
Sasha.