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 com.sun.java.swing 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 com.sun.java.swing.*;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);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS