JDK-4352991 : Clipping of text when printing
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2000-07-13
  • Updated: 2000-07-19
  • Resolved: 2000-07-19
Related Reports
Duplicate :  
Description

Name: sl110371			Date: 07/13/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

It seems that JTextArea doesn't print to a printer properly.  When the JTextArea
is painted on the screen, everything looks fine.  When looking at the output
from a printer or printed file, the right side of the text area is clipped.
Below is some sample code to run:

// start sample code

import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.border.*;

public class ClippingBug extends JFrame implements Printable
{    
    JTextArea text;
    JLabel label;
    JPanel holder;

    public ClippingBug( )
    {
        RepaintManager.currentManager( this ).setDoubleBufferingEnabled(
            false );
        Font theFont = new Font( "SansSerif", Font.PLAIN, 8 );

        holder = new JPanel( new GridLayout( 1, 2 ) );
        text = new JTextArea( "The quick brown fox jumped over the lazy dog." +
            "All work and no play makes Jack a dull boy.  Dark is the " +
            "suede that mows like a harvest." );
        label = new JLabel( "This is to take up space." );
        label.setBorder( new LineBorder( Color.red ) );
        
        text.setLineWrap( true );
        text.setWrapStyleWord( true );
        text.setFont( theFont );

        holder.add( text );
        holder.add( label );

        getContentPane( ).setLayout( new GridLayout( 2, 1 ) );
        getContentPane( ).add( holder );
        pack( );
        setSize( 400, 400 );
        setVisible( true );
    }
	
    public int print( Graphics g2, PageFormat pf, int pageIndex )
    {
        if ( pageIndex > 1 )
        {
            return( Printable.NO_SUCH_PAGE );
        }
		
        Graphics2D g = (Graphics2D)g2;
        g.translate( pf.getImageableX( ), pf.getImageableY( ) );
		
        g.translate( holder.getX( ), holder.getY( ) );
        holder.paint( g );
        g.translate( -holder.getX( ), -holder.getY( ) );

        return( Printable.PAGE_EXISTS );
    }
	
    public void doPrint( )
    {
        PrinterJob pj = PrinterJob.getPrinterJob( );
	    
        pj.setPrintable( this );

        if( pj.printDialog( ) )
        {
            try
            {
                pj.print( );
            }
            catch( PrinterException ee )
            {
                System.out.println( ee );
            }
        }
    }
	
    public static void main( String args[] )
    {
        ClippingBug a = new ClippingBug( );
        a.doPrint( );
        System.exit( 0 );
    }
}

// end sample code

Running the code shows that the entire JTextArea is printed, but some of the
words are clipped on the right hand side.  By placing a border around the text
area, you can see that it isn't getting painted over by the JLabel filling in
the right hand side of the grid.  Changing the UI does not improve results.

This is a new problem that was introduced when 1.3 came out of Beta.  Similar
results have been observed using JEditorPane and JTextPane.  Also, creating your
own simple TextArea class by extending JComponent and writing a UI does not
solve the problem.  The custom component is still clipped on the right hand side
when printed, but not when it is painted to the screen.  Running the same code
on 1.3beta results in the entire JTextArea being printed, with no clipping on
the right hand side.
(Review ID: 107131) 
======================================================================

Comments
EVALUATION This is a dup of 4352983.
11-06-2004

WORK AROUND Name: sl110371 Date: 07/13/2000 None ======================================================================
11-06-2004