0
package Rainbow;

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

public class Entry
{
    public static void main(String[] Q)
    {
        JFrame R = new JFrame();
        JPanel P = new JPanel()
        {
            public Dimension getMaximumSize()
            { return new Dimension(Integer.MAX_VALUE,getMinimumSize().height); }
        };
        P.setLayout(new BoxLayout(P,BoxLayout.Y_AXIS));
        JTextArea A = new JTextArea(
                "VERYLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONG");
        A.setEditable(false);
        A.setLineWrap(true);
        P.add(new Label("Text"));
        P.add(A);
        JScrollPane S = new JScrollPane(P);
        R.add(S);
        R.setSize(300,300);
        R.setLocationRelativeTo(null);
        R.setVisible(true);
    }
}

It is how it looks on startup. StartUp

After reducing the Frame's size. Reduce The JTextArea inside would not reduce its size.

I'm using JTextArea here because it seems to be the easiest way to do line wrap on a component. So how to solve it? Or is there an alternate way to do the same thing?

ZED.CWT
  • 9
  • 1
  • 1

1 Answers1

0

You need to resize TextArrea too. Here are some related examples for your question: Java JTextArea that auto-resizes and scrolls.

Example described where JPanel is used and how we can do it:

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());  //give your JPanel a BorderLayout

JTextArea text = new JTextArea(); 
JScrollPane scroll = new JScrollPane(text); //place the JTextArea in a scroll pane
panel.add(scroll, BorderLayout.CENTER); //add the JScrollPane to the panel
// CENTER will use up all available space
Community
  • 1
  • 1
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38