JTextArea dimensions
If I have:
JTextArea jta =new JTextArea("Lots O' Text..........and some more text");
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
How do I find out what the correct height of the textbox would be for a width of say...100.
getPreferredSize doesnt account for the line wraps.
[442 byte] By [
Nethera] at [2007-11-26 17:10:08]

# 1
A text area doesn't have a preferred size until the frame has been "realized" which basically means its visible or you've invoked pack() on the frame:
import java.awt.*;
import javax.swing.*;
public class TextAreaFixed extends JFrame
{
public TextAreaFixed()
{
JTextArea textArea = new JTextArea();
textArea.setColumns(15);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.setText("one two three four five six seven eight nine ten");
getContentPane().add( textArea );
System.out.println("Before Pack: " + textArea.getPreferredSize());
pack();
System.out.println("After Pack: " + textArea.getPreferredSize());
pack();// now that we have true preferred size, lets pack again
setLocationRelativeTo( null );
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
new TextAreaFixed();
}
}
# 3
> I dont get why you need to pack twice.
Neither do I. Its just the way the layout of the text area works.
> Also, how would I do this if I just want to interact with a JComponent
> as the holder of the jtextarea and not the jframe?
The layout of a component or container is only done when its part of a visible GUI.