Skip to main content

APIzation: Replication Package

[Q#4083322][A#4083850] How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?

https://stackoverflow.com/q/4083322

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!

Answer

https://stackoverflow.com/a/4083850

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().

APIzation

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() );
package com.stackoverflow.api;

import javax.swing.*;

public class Human4083850 {

  public static JTextArea createJTextArea(int width, String text) {
    JTextArea textArea = new JTextArea();
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setText(text);
    System.out.println("000: " + textArea.getPreferredSize());
    textArea.setSize(width, 1);
    System.out.println(width + ": " + textArea.getPreferredSize());
    textArea.setSize(textArea.getPreferredSize());
    return textArea;
  }
}

package com.stackoverflow.api;

import javax.swing.JTextArea;

/**
 * How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/4083850">https://stackoverflow.com/a/4083850</a>
 */
public class APIzator4083850 {

  public static void createJtextarea() throws Exception {
    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());
  }
}