United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4202291 long lines in wrapped JTextArea causes severe performance degradation
JDK-4202291 : long lines in wrapped JTextArea causes severe performance degradation

Details
Type:
Bug
Submit Date:
1999-01-12
Status:
Resolved
Updated Date:
2005-04-11
Project Name:
JDK
Resolved Date:
2004-12-01
Component:
client-libs
OS:
windows_nt,solaris_2.5.1,windows_95,windows_2000
Sub-Component:
javax.swing
CPU:
x86,sparc
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.1.7,1.2.2,1.3.0,1.4.2
Fixed Versions:

Related Reports
Duplicate:
Duplicate:

Sub Tasks

Description
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

                                    

Comments
WORK AROUND

Name: krT82822			Date: 01/11/99


always add a carriage return at the end of a long line of text.
======================================================================
###@###.### 11/4/04 11:22 GMT
                                     
4-11-04 00:00:00
EVALUATION

The layout of each logical line (i.e. area in the model that ends with a 
newline) is computed in the view on the fly.  If the number of wrapped 
pieces in the view is large is is effectively recomputing all of the 
pieces each time it paints.   Attempting to display one logical line wrapped
into hundreds or thousands of pieces will result in very low performance
with the current implementation.  This could be fixed by switching the
views used to display a wrapped JTextArea to ZoneView and ParagraphView
and fixing bug4250864.  
timothy.prinzing@eng 1999-09-17

Name: pzR10082			Date: 04/30/2004


Currently a line is wrapped each time it is painted, or viewToModel/
modelToView is called. This makes a significant slowdown in when 
there are many long lines.

A solution is to cache calculated line break positions in an array,
and reuse them until they become invalid.

###@###.###

======================================================================
                                     
2004-09-27
SUGGESTED FIX

http://sa.sfbay.sun.com/projects/swing_data/mustang/4202291.4
###@###.### 11/4/04 11:22 GMT
                                     
2004-11-04



Hardware and Software, Engineered to Work Together