🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #1594

Re: Printing the contents of JEditorPane or JTextField

#1594
From: Tad Frysinger [TeamB]
Date: Tue, 21 Oct 2003 16:34
92 lines
4395 bytes
lredmond wrote:

> I implement a class which implents the Printable inteface, my problem is it
> only prints the visible portion of the text in the pane or textarea.  How do
> I print all the contents.  I have searched the web and it seems like everyon
> is running into the same problem where only the visible portion of text is
> printing and not the complete text.
> 
> lans
> 
> 
This is because your class is probably using the default graphics 
context, which clips to the component's visible area. I believe the 
technique you need to use is to adjust that clipping area prior to 
painting it. The "adjustment" needs to take into account the appropriate 
width of the text as compared to the number of pixels available on the 
device to which you are printing.

Here is a butt-simple utility class I adapted from some other code, I 
think originally from Marty Hall for public consumption:

public class ComponentPrinter implements Printable {
   private Component componentToPrint;
   private double cHeight;
   private int totalNumPages = 0;
   public ComponentPrinter(Component component) {
      componentToPrint = component;
      cHeight = component.getSize().height;
   }

   public void print() {
     PrinterJob printJob = PrinterJob.getPrinterJob();
     PageFormat pf1 = new PageFormat();
     double pageHeight = pf1.getImageableHeight();
     totalNumPages = (int) Math.ceil(cHeight/pageHeight);

     printJob.setPrintable(this, pf1);
     try {
        printJob.print();
     }
        catch (PrinterException pe) {
           System.out.println("Error printing: " + pe);
     }
   }

   public int print(Graphics g, PageFormat pageFormat, int pageIndex) {

     if ( pageIndex >= totalNumPages ) {
        return Printable.NO_SUCH_PAGE;
     }

     Graphics2D g2d = (Graphics2D) g;
     g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
     disableDoubleBuffering(componentToPrint);
     componentToPrint.paint(g2d);
     enableDoubleBuffering(componentToPrint);
     return (PAGE_EXISTS);
   }

   private void disableDoubleBuffering(Component c) {
     RepaintManager currentManager = RepaintManager.currentManager(c);
     currentManager.setDoubleBufferingEnabled(false);
   }

   private void enableDoubleBuffering(Component c) {
     RepaintManager currentManager = RepaintManager.currentManager(c);
     currentManager.setDoubleBufferingEnabled(true);
   }

}


A couple things to note: the double-buffering business is for speed 
optimiation. Also, note that I only calculate the height stuff once, 
prior to the actual print() method. In a real-world environment, you may 
have to do this on each print pass.

In fact, I have done no checking in the above code to see whether or not 
the combination of page margins and device margins is going to cause the 
text to be clipped, which is normally something you'd do. For a 
JTextArea or other text components, I'd probably figure out my max 
column width based on the FontMetrics involved and set it on the 
component, so the wrapping would be done PRIOR to calling this class, so 
you don't have to do any fancy measuring on the fly.

Here is another link to help you out:

http://www.manning.com/Robinson/chapter22.pdf


-- 
Tad Frysinger [TeamB]
Technical Details
Message-ID: <3f956014$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3f8b3eb2@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,f34b5518ea4bc13d
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2003-10-21 09:43:54 PST
Date: Tue, 21 Oct 2003 10:34:13 -0600
From: "Tad Frysinger [TeamB]" <tfrysinger@earthlink.net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing
Subject: Re: Printing the contents of JEditorPane or JTextField
References: <3f8b3eb2@newsgroups.borland.com>
In-Reply-To: <3f8b3eb2@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 206.61.185.113
Message-ID: <3f956014$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1066754068 206.61.185.113 (21 Oct 2003 09:34:28 -0700)
Lines: 93
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail