Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4202291

long lines in wrapped JTextArea causes severe performance degradation

XMLWordPrintable

    • b14
    • x86, sparc
    • solaris_2.5.1, windows_95, windows_nt, windows_2000

      Name: krT82822 Date: 01/11/99


      JTextArea setLineWrap causes severe performance degradation

      If line wrap is true, and a large line of text is set into the text area, then painting performance can suffer if the line of text doesn't end in a carriage return. The example program demonstrates the problem. After pressing the 'Bad Fill' button, try selecting text or resizing the window and notice that it takes a lot longer than it should. This bug is new to Swing-1.1 and doesn't exist in Swing-1.0.3.

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      /**
       * Demonstrates a performance bug in JTextArea if line wrap
       * is true, and a large line of text is entered. Oddly, if
       * the line of text ends in a carriage return, performance
       * isn't affected.
       */
      public class JTextAreaBug extends JPanel
      {
          JTextArea text = new JTextArea();
        
          public static void main( String[] args )
          {
              JFrame frame = new JFrame( "JTextArea Bug" );
              frame.addWindowListener(
                  new WindowAdapter()
                  {
                      public void windowClosing( WindowEvent evt )
                      {
                          System.exit( 0 );
                      }
                  });

              frame.getContentPane().add( new JTextAreaBug(), BorderLayout.CENTER );
              frame.setSize( 400, 300 );
              frame.setVisible( true );
          }
          
          public JTextAreaBug()
          {
              super( new BorderLayout() );
              text.setLineWrap( true );
              
              add( new JScrollPane( text ), BorderLayout.CENTER );

              JButton goodFill = new JButton( "Good Fill" );
              JButton badFill = new JButton( "Bad Fill" );
              JPanel buttonPanel = new JPanel();
              buttonPanel.add( goodFill );
              buttonPanel.add( badFill );
              add( buttonPanel, BorderLayout.SOUTH );
              
              goodFill.addActionListener(
                  new ActionListener()
                  {
                      public void actionPerformed( ActionEvent evt )
                      {
                          fillText( true );
                      }
                  });
              badFill.addActionListener(
                  new ActionListener()
                  {
                      public void actionPerformed( ActionEvent evt )
                      {
                          fillText( false );
                      }
                  });
          }
         
          /**
           * Fills the text field with bunch of characters. If
           * addCarriageReturn is true, then a carriage return
           * is added at the end.
           */
          void fillText( boolean addCarriageReturn )
          {
              final int SIZE = 27000;
              StringBuffer buff = new StringBuffer( SIZE + 1 );
              for( int i = SIZE - 1; i >= 0; i-- )
                  buff.append( "x" );
              
              if( addCarriageReturn )
                  buff.append( '\n' );
              
              text.setText( buff.toString() );
          }
      }
      (Review ID: 47890)
      ======================================================================
      ###@###.### 11/1/04 10:39 GMT

            peterz Peter Zhelezniakov
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: