Java Program to set JTextArea to wrap by word in Java

Java 8Object Oriented ProgrammingProgramming

To set JTextAream to wrap by word, you need to use the setWrapStyleWord(). Let’s say we have created a new JTextArea and set demo text −

JTextArea textArea = new JTextArea("This is a text displayed for our example.
More content is added in it now. More content is added in it now. We will now wrap this text!!!!!!!!!!!!!!!!!!!");

Now, wrap by word and set it TRUE −

textArea.setWrapStyleWord(true)

The following is an example to set JTextArea to wrap by word −

Example

package my;
import java.awt.GridLayout;
import javax.swing.*;
public class SwingDemo {
   SwingDemo() {
      JFrame frame = new JFrame("Demo");
      JTextArea textArea = new JTextArea("This is a text displayed for our example.
      More content is added in it now. More content is added in it now. We will now wrap this text!!!!!!!!!!!!!!!!!!!");
      textArea.setLineWrap(true);
      textArea.setWrapStyleWord(true);
      frame.add(textArea);
      frame.setSize(550,300);
      frame.setLayout(new GridLayout(2, 2));
      frame.setVisible(true);
   }
   public static void main(String args[]) {
      new SwingDemo ();
   }
}

Output

raja
Published on 31-May-2019 12:45:40
Advertisements