Search code examples
javaswingjtextarea

Java Swing JTextArea Horizontal Constraint only


Is there a way in java swing to code:

"Constraint the width of JTextArea to W but allow JTextArea to expand vertically to contain all the linewrapped text'?


Solution

    1. Set the text area to W columns.
    2. Set the wrap style & line wrap.
    3. Add it to a scroll pane with a vertical scroll bar as needed, but no horizontal scroll bar.

    Alternately if the content does not need to be selectable or editable, use a label with HTML & CSS to constrain the width.