Trey Collier
Trey Collier

Reputation: 129

How to make my JTextArea or JTextPane automatically create a new line?

In my program, I want a large area for typing in. I have been playing around with JTextPanes and JTextAreas. However if I continuously type, when I reach the end of the area, it does not automatically create a new line it just continues off the box, I know that I can add a horizontal scroll bar, but I would prefer it to just drop to a new line when I reach the end of the box. I saw something called dropMode and tried playing with this to see if it helped, but it just resulted in the following error "Unsupported drop mode for text". My program is GUI based, therefore the user will be typing directly into the area, not appending it.

Upvotes: 3

Views: 443

Answers (1)

XtremeBaumer
XtremeBaumer

Reputation: 6435

textArea.setLineWrap(true) should work as you want.

Example:

public Etst() {
    JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    JTextArea area = new JTextArea();
    area.setLineWrap(true);
    f.add(area);
    f.setSize(100, 100);
    f.setVisible(true);
}

Upvotes: 3

Related Questions