Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: User Interface Design
Expertise: Beginner
Sep 15, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 keystroke events 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 line wrapping 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 you to change the line wrapping style through the setLineWrapStyleWord(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!");

    __scrollPane = new JScrollPane();

    __lineWrapButton    = new JCheckBox("Line Wrap On", true);
    __lineWrapStyleButton = new JCheckBox("Word Wrap Style On", true);

    lineListener = new ActionListener() {
      public void actionPerformed(ActionEvent event) {
	AbstractButton button = (AbstractButton)event.getSource();
	// 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.

    styleListener = new ActionListener() {
      public void actionPerformed(ActionEvent event) {
	AbstractButton button = (AbstractButton)event.getSource();

    __buttonPanel = new JPanel();

    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();

    wrapper = new WrapText();

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date