Wrapping Text in TextArea

Wrapping Text in TextArea

Is there a way to wrap text that is written to the TextArea?

The java.awt.TextArea class does not have built-in support for wrapping text, but you can implement line wrapping by catching keystrokeevents and tracking the current caret position and comparing it to the size of the TextArea. It doesn’t make much sense to go through the hassle though, since the JTextArea class in the package has already implemented line wrapping. You can turn linewrapping on and off with setLineWrap(boolean). An argument of true turns line wrapping on and an argument of false turns line wrapping off. By default, JTextArea has line wrapping turned off. But there is more than one style of line wrapping. The default style of line wrapping is to continue the characters being typed onto the next line without regard to word breaks. However, in a text editor, you will usually want to wrap lines based on word breaks. JTextArea allows youto change the line wrapping style through thesetLineWrapStyleWord(boolean) method. An argument of true changes the line wrapping style to be based on word-based, while an argument of false makes it character-based. The following program shows how to use these methods. It creates a text area in which you can type and provides two checkboxes that let you turn line wrapping on and off as well as toggle the style of line wrapping.

import java.awt.*;import java.awt.event.*;import*;public final class WrapText extends JFrame {  private JTextArea __textArea;  private JScrollPane __scrollPane;  private JToggleButton __lineWrapButton;  private JToggleButton __lineWrapStyleButton;  private JPanel __buttonPanel;  public WrapText() {    super("Wrap Text Demo");    Container contentPane;    ActionListener lineListener, styleListener;    __textArea = new JTextArea("Start typing!");    __textArea.setColumns(30);    __textArea.setRows(20);    __textArea.setLineWrap(true);    __textArea.setWrapStyleWord(true);    __scrollPane = new JScrollPane();    __scrollPane.getViewport().add(__textArea);    __lineWrapButton    = new JCheckBox("Line Wrap On", true);    __lineWrapButton.setRequestFocusEnabled(false);    __lineWrapStyleButton = new JCheckBox("Word Wrap Style On", true);    __lineWrapStyleButton.setRequestFocusEnabled(false);    lineListener = new ActionListener() {      public void actionPerformed(ActionEvent event) {	AbstractButton button = (AbstractButton)event.getSource();	__textArea.setLineWrap(button.isSelected());	// The following is a kludge to work around what might be a	// bug in Swing.  After you modify the line wrapping while the	// JTextArea is visible, it doesn't update; so we repaint it.	__textArea.repaint();      }    };    styleListener = new ActionListener() {      public void actionPerformed(ActionEvent event) {	AbstractButton button = (AbstractButton)event.getSource();	__textArea.setWrapStyleWord(button.isSelected());	__textArea.repaint();	__textArea.requestFocus();      }    };        __lineWrapButton.addActionListener(lineListener);    __lineWrapStyleButton.addActionListener(styleListener);    __buttonPanel = new JPanel();    __buttonPanel.add(__lineWrapButton);    __buttonPanel.add(__lineWrapStyleButton);    contentPane = getContentPane();        contentPane.setLayout(new BorderLayout());    contentPane.add(__scrollPane, BorderLayout.CENTER);    contentPane.add(__buttonPanel, BorderLayout.SOUTH);  }  public static void main(String[] args) {    WrapText wrapper;    WindowListener exitListener;    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {	Window window = e.getWindow();	window.setVisible(false);	window.dispose();	System.exit(0);      }    };    wrapper = new WrapText();    wrapper.addWindowListener(exitListener);    wrapper.pack();    wrapper.setVisible(true);  }}
See also  The Art of AI-Generated Meeting Minutes

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist