JTextArea Scrolling
Home ] Up ] JTextComponent ] JTextField ] JPasswordField ] JTextArea ] The Document Model ] [ JTextArea Scrolling ] JTextArea Editing ]

 

 

Scrolling and Wrapping Text in a Text Area

You may have noticed that the text areas shown earlier do not have much of a visible border.  In addition, if you add more lines of text to a text area, it increases its vertical size, which is not usually a desirable feature.   In most cases, you'd like to have the text area scroll appropriately, without changing size.

To do this, you place the text area in a JScrollPane, and then add the scroll pane to the panel:

package swingExamples;

import java.awt.BorderLayout;
import java.awt.Container;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

class ScrollingTextAreasPanel extends JPanel
{
  public ScrollingTextAreasPanel()
  {
    setLayout(new BorderLayout());
    add(new TextAreaPanel(), BorderLayout.CENTER);
    add(new InputPanel(), BorderLayout.SOUTH);
  }
  
  /////// Private data /////
  private JTextArea m_textArea = new JTextArea(8, 40);
  private JButton m_insertButton = new JButton("Insert");
  private JButton m_wrapButton = new JButton("Wrap");
  private JButton m_noWrapButton = new JButton("No Wrap");
  private JButton m_replaceButton = new JButton("Replace");
  
  /////// Inner classes /////
  class TextAreaPanel extends JPanel
  {
    public TextAreaPanel()
    {
      setLayout(new BorderLayout());
      // Place text area within a scroll pane
      m_textArea = new JTextArea(8, 40);
      JScrollPane scrollPane = new JScrollPane(m_textArea);
      scrollPane.setVerticalScrollBarPolicy(
          JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
      scrollPane.setHorizontalScrollBarPolicy(
          JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      add(scrollPane, BorderLayout.CENTER);
    }
  }
  
  class InputPanel extends JPanel
  {
    public InputPanel()
    {
      add(m_insertButton);
      m_insertButton.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ev)
        {
          m_textArea.append(
              "The quick brown fox jumps over the lazy dog. ");
        }
      }
      );
      add(m_wrapButton);
      m_wrapButton.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ev)
        {
          m_textArea.setLineWrap(true);
        }
      }
      );
      add(m_noWrapButton);
      m_noWrapButton.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ev)
        {
          m_textArea.setLineWrap(false);
        }
      }
      );
    }
  }
}

class ScrollingTextAreasFrame extends JFrame
{
  public ScrollingTextAreasFrame()
  {
    setTitle("ScrollingTextAreas");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.add( new ScrollingTextAreasPanel() );
  }
}

public class ScrollingTextAreas
{
  public static void main(String[] args)
  {
    ScrollingTextAreasFrame frame = 
                        new ScrollingTextAreasFrame();
    frame.setVisible(true);
  }
}

which produces:

 

This page was last modified on 02 October, 2007