How do you print all the contents of a JPanel?????

I have produced a java application and want to add printing facilities to the application.
I want the user to be able to a print a page (JPanel) which consists of JLabel's, JTextarea's and JTable's. How can this be done????
Thank you,
Yuvraj.

Just remember to scale the JPanel to your printer size. This is not originally my code, but I have used it and it does the trick. There are some custom changes you can pull out if you don't want/need them.
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
/** A simple utility class that lets you very simply print
*  an arbitrary component. Just pass the component to the
*  PrintUtilities.printComponent. The component you want to
*  print doesn't need a print method and doesn't have to
*  implement any interface or do anything special at all.
*  <P>
*  If you are going to be printing many times, it is marginally more
*  efficient to first do the following:
*  <PRE>
*    PrintUtilities printHelper = new PrintUtilities(theComponent);
*  </PRE>
*  then later do printHelper.print(). But this is a very tiny
*  difference, so in most cases just do the simpler
*  PrintUtilities.printComponent(componentToBePrinted).
public class CPrintUtilities implements Printable
  private Component componentToBePrinted;
  protected Book m_book = new Book();
  protected ArrayList m_arrayPages  = new ArrayList();
  protected double m_scaleFactorX = 1.0f;
  protected double m_scaleFactorY = 1.0f;
  protected boolean m_bShowPrintDialog = true;
  protected static PrinterJob printJob = null;
  public static void printComponent(Component c)
    new CPrintUtilities(c).print();
  public CPrintUtilities(Component componentToBePrinted)
    this.componentToBePrinted = componentToBePrinted;
  public CPrintUtilities()
  public CPrintUtilities(Book book)
    m_book = book;
  public void AddPage(Component componentToBePrinted)
     m_arrayPages.add(componentToBePrinted);
  public void SetShowPrintDialog(boolean bShowPrintDialog)
     m_bShowPrintDialog = bShowPrintDialog;
//   public void print()
   public boolean print()
      printJob = PrinterJob.getPrinterJob();
//      if(printJob.printDialog()) {
      if(m_bShowPrintDialog) {
         if(!printJob.printDialog())  {
            return false;
      try {
         // Revise the Page Format
         PageFormat format = printJob.defaultPage();
         double nMargin = 72/4;  // Inch / 4 or one quarter inch
         Paper paper = format.getPaper();
         double nHeight = paper.getHeight() - (nMargin * 2);
         double nWidth  = paper.getWidth()  - (nMargin * 2);
         double scaleFactorX = 1.0f;
         double scaleFactorY = 1.0f;
         paper.setImageableArea(nMargin,nMargin,nWidth,nHeight);
         format.setPaper(paper);
         for(int x = 0;x < m_arrayPages.size();x++) {
            componentToBePrinted = (Component)m_arrayPages.get(x);
            // Scale to print
            double nCompHeight = componentToBePrinted.getHeight();
            double nCompWidth  = componentToBePrinted.getWidth();
            if (nWidth > nCompWidth)
              scaleFactorX = nCompWidth / nWidth;
            else
              scaleFactorX = nWidth / nCompWidth;
            if (nHeight > nCompHeight)
              scaleFactorY = nCompHeight / nHeight;
            else
              scaleFactorY = nHeight / nCompHeight;
            m_scaleFactorX = scaleFactorX;
            m_scaleFactorY = scaleFactorY;
            printJob.setPrintable(this,format);
            printJob.print();
      catch(PrinterException pe) {
         CSystem.PrintDebugMessage("Error printing: " + pe);
      return true;
   public void printBook()
      PrinterJob printJob = PrinterJob.getPrinterJob();
      if (printJob.printDialog())
         try
            for(int x = 0;x < m_book.getNumberOfPages();x++)
               Printable printable = m_book.getPrintable(x);
               printJob.setPrintable(printable);
               printJob.print();
         catch(PrinterException pe)  {
            CSystem.PrintDebugMessage("Error printing: " + pe);
  public int print(Graphics g, PageFormat pageFormat, int pageIndex)
    if (pageIndex > 0)
      return(NO_SUCH_PAGE);
    else
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
      g2d.scale(m_scaleFactorX,m_scaleFactorY);
      disableDoubleBuffering(componentToBePrinted);
      componentToBePrinted.paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
  /** The speed and quality of printing suffers dramatically if
   *  any of the containers have double buffering turned on.
   *  So this turns if off globally.
   *  @see enableDoubleBuffering
  public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  /** Re-enables double buffering globally. */
  public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);

Similar Messages

  • How do you print all the photos in your flagged file? I don't want to make these photos into a new event.

    I want to order several photos. I have flagged them and there is no option to select all and order the prints. I don't want to create a new event with these photos, as I want them in their original event files.
    How are multiple photos ordered/exported for printing?

    Go to your Flagged Photos Smrt Album on the Left.
    Command - a will select All
    Then File -> New -> Album
    Regards
    TD

  • How can i print all the contect of the code in sapscript window ? ?

    how can i print all the contect of the code in sapscript window ? ?

    Hi,
    Do you mean that you want to print the ABAP code to SAPscrip form ?
    Svetlin

  • How can i print all the tab pages not just the first page (tab)

    how can i print all the tab pages not just the first page (tab)

    You would need to do this programmatically. Here's one way:
    Attachments:
    Example_VI_BD6.png ‏3 KB

  • How do I print all the open tabs in Photoshop, without clicking on each and printing manually?

    How do I print all the open tabs in Photoshop, without clicking on each and printing manually?

    If the song are part of an album, the album should have a cloud icon for downloading it in full. If they are separate tracks (not in the same album) you will have to download individually.

  • How do I tag all the contents of a folder?

    How do I tag all the contents of a folder?
    I'm trying to add an archive tag to a lot of files and folders, and is it possible to just tag the parent folder and all files and folders within it to adopt this tag, so that when I come to search for a document later on, and it has an archive tag, I know to dismi

    Ideally, the program you want to delete has an 'uninstaller' application and that should delete all the files. If that isn't available you could download and run this free app: http://www.freemacsoft.net/appcleaner/
    Probably not as thorough but it will do the job of ridding you mac of most of the files associated with the app you no longer want.

  • How can you delete all the e mail at one time instead of one at a time?

    How can you delete all the e mail at one time instead of one at a time?

    you'd have to do it from the computer,

  • Why has iTunes seperated a album which I have copied over from my external hard drive into iTunes? How can you pull all the songs back together into one album again?

    Why has iTunes seperated a album into indevidual songs, which I have copied over from my external hard drive into iTunes? How can you pull all the songs back together into one album again?

    Steve MacGuire a.k.a. turingtest2 - iTunes & iPod Hints & Tips - Grouping Tracks Into Albums - http://www.samsoft.org.uk/iTunes/grouping.asp (older post on Apple Discussions http://discussions.apple.com/message/9910895)
    Quick answer:  Select all the tracks on the album, File > get info, and either give them all a single "album artist", or check the "compilation" flag (as in https://discussions.apple.com/message/17670085).
    If these are from multiple-CD sets you may also need to enter the appropriate information in the disc number fields.  If they are not a multiple CD set you still need to make sure the disc number fields are set correctly or all empty.

  • How do you delete all the messages in your search results in Thunderbird 31.2?

    I'm trying to clean out my inbox. When I search for a given term in the global search box and get my results, then I choose "Select All." Then I choose "Move to Trash," and nothing happens.
    How do you delete all the messages in your search results in Thunderbird 31.2?

    I'm trying to clean out my inbox. When I search for a given term in the global search box and get my results, then I choose "Select All." Then I choose "Move to Trash," and nothing happens.
    How do you delete all the messages in your search results in Thunderbird 31.2?

  • How can I print all the open tabs from the Reader?

    One of my users subscirbes WSJ Online and uses the Reader to read articles. The user would like to know if he can print all the open tabs in Reader at once instead of one tab or article at a time.
    OS X10.8.2

    Hi,
    Do you mean that you want to print the ABAP code to SAPscrip form ?
    Svetlin

  • How can I encrypt all the content of my Ipad 2?

    Hello,
    I am looking for a solution like CheckPoint FDE in order to encrypt all the content of my Ipad 2?
    Does anyone know a similar solution?
    Thanks

    According to apple's spec sheet it is encrypted at rest. I was also on a conference call on Monday with an Apple security analyst and he confirmed that is is encrypted at rest. Make sure you set the passcode lock and  use a strong password and make sure if you back it up to your hard drive that you choose encrypt on backup.

  • How can I delete all the contents on my icloud ?

    I tried to setup icloud for my iphone and mac today.. disaster.. it kinda messed up my ical and contact ( lucky i got back up file). so I unlinked my computer and phone to icloud and restored all the data.
    Next step, I would like to clear all the contents in my icloud.  Is there a way to do it rather than delete the item manually? 

    anyone  ? please?

  • How do I transfer all the content from my old 3rd gen to my new 4th gen iTouch, including apps and notes. I replaced an old 2nd gen with the 3rd last year and lost all my contacts and notes.

    What's the best way to transfer all the content, including notes and apps, from my old 3rd gen to my new 4th gen. When I replaced my last iTouch with the 3rd gen I lost all my contacts and notes.

    Backup the 3g by syncing in iTunes. Then connect the new one and sync everything to it from your backup.
    IPod Touch user guide

  • Can you print all the tab control's contents and include the tabs?

    We are trying to generate a report that includes the contents of a tab control (graphs, text).  Using the Append Control Image to Report.vi, we are only able to print out the active tab and the tab is cut off of the image. How can we generate reports that include all the tab pages and their contents and include the labeling tabs?
    Thanks in advance.
    Jim

    I do have a workaround for makinging the prints but it looks like some sort of cropped screensave that cuts off the tabs.  In my sample I was printing out 3 of the tab controls and you can see there are three by the cut off tabs.  Is there a setting to allow for more of the graph and its surroundings to be "captured"?
    Attachments:
    Document.pdf ‏53 KB
    Document (2).pdf ‏51 KB
    Document (3) (2).pdf ‏58 KB

  • How do you print from the text (multiline) option so that all of the text is included?

    I'm trying the trial version of formscentral and have made a form. In this form I used the text (multiline) option. I saved the form as a pdf and filled it out to test it. When I go to print the form the only text that appears is what's visible on the screen. How do I print the form so that all of the information is depicted?

    This is something we are working on fixing but in the mean time what you can do is one of two things :
    1) make the multiline fields taller so that it would fit all the text in the responses.
          You can do that by going to the Desing Tab and dragging the bottom right corner of a multiline text fields to make them bigger.
    2) print the Detail View instead.
          From the View Responses Tab go to View > Detail View.
          Select the response you want to print
          Press on the printer icon at the bottom of the Detail View.
          This will print the data in a name/value pair format.
    Gen

Maybe you are looking for

  • HP Envy 17334 Touchpad issues

    I wanted to let you know these are the things my touchpad has been doing:  -Very sluggish when first logging in to the computer. -Randomly opens up windows that the arrow hovers over but I don't click on. -Jumps around. -Makes the window half screen.

  • How Can I Burn a Disk with PE11?  Or do I use Media Player?

    Any help would be appreciated!

  • Error while migrating to Sun Directory Server 6.0

    Hi All, I am trying to migrate the Sun One Directory Server 5.2 to Sun Directory Server 6.0. I am getting the following error bash-3.2# ./dsmig migrate-config /var/Sun/mps/slapd-circb2bld3/ /var/SunDirectoryServer6.0/dsInst/ Launching Configuration M

  • Image size too big after drag and drop from iphoto

    I capture around 30 images/day with a point and shoot digital camera (5 MB/image). These images are used as content for about 10 documents/day that I create in Pages. After importing them into the computer I convert to grayscale and resize (via batch

  • Supplier performance evaluation

    Dear Guru's Can we include supplier survey response and transaction data from SAPSRM/ECC to determine the KPI of suppliers? Can we also include the supplier status like blocked or black listed, expired in the report? Can we include category of suppli