Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?

In all the examples that I can find that use a JTextArea, the height & width is known before constructing the JTextArea, and if the JTextArea would require more height, then it is put inside of a JScrollPane. Obviously, the height of JTextArea is dependent on the width and the text contents.

Now, my situation requires that I do not use a JScrollPane, but instead that the JTextArea be just tall enough to display all the text. When I create the JTextArea, I know the text contents and how much width it will have to work with; I don't know the height - I want that to be as small as possible without cutting off any of the text. This seems very difficult to accomplish.

As a side note, the JTextArea will be added to a JPanel that does not have a layout manager - it uses absolute positioning based on the added component's preferred size. This requires that my JTextArea would return the correct dimensions on getPreferredSize(). The correct dimensions should be the width that I provided when I constructed it, and the minimum height that is required to display all the text with the provided width.

I've found some similar threads that discuss the oddities/bugs involved with the JTextArea that are sometimes solved by calling pack() twice on the parent container. This is not an option for me. I'm tempted to basically create my own JTextArea that takes a width and String and computes the necessary minimum height based on the width and font settings, but I figured I would ask around first before spending the time to do that.

Hopefully my question is clear. Thank you all for your help!

like image 921
Nate W. Avatar asked Nov 03 '10 00:11

Nate W.


2 Answers

it uses absolute positioning based on the added component's preferred size.

Sounds like the job of a layout manager.

This requires that my JTextArea would return the correct dimensions on getPreferredSize().

JTextArea textArea = new JTextArea();
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.setText("one two three four five six seven eight nine ten");
System.out.println("000: " + textArea.getPreferredSize());
textArea.setSize(100, 1);
System.out.println("100: " + textArea.getPreferredSize());
textArea.setSize( textArea.getPreferredSize() );
like image 65
camickr Avatar answered Sep 22 '22 13:09

camickr


import java.awt.*;
import javax.swing.*;

class FixedWidthLabel {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                String pt1 = "<html><body width='";
                String pt2 =
                    "px'><h1>Label Height</h1>" +
                    "<p>Many Swing components support HTML 3.2 &amp;" +
                    " (simple) CSS.  By setting a body width we can cause the " +
                    " component to find the natural height needed to display" +
                    " the component.<br><br>" +
                    "<p>The body width in this text is set to " +
                    "";
                String pt3 =
                    " pixels." +
                    "";

                JPanel p = new JPanel( new BorderLayout() );

                JLabel l1 = new JLabel( pt1 + "125" + pt2 + "125" + pt3 );
                p.add(l1, BorderLayout.WEST);

                JLabel l2 = new JLabel( pt1 + "200" + pt2 + "200" + pt3 );
                p.add(l2, BorderLayout.CENTER);

                JOptionPane.showMessageDialog(null, p);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
like image 25
Andrew Thompson Avatar answered Sep 21 '22 13:09

Andrew Thompson