3 Replies - 3390 Views - Last Post: 07 June 2012 - 03:29 PM

#1 ramprasadm   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 22-December 07

Java Printing

Posted 22 December 2007 - 09:17 PM

Description: Save this java file and use its static method as below for printing PrintUtilities.printComponent(p3); where p3 is a component..It takes a component and prints it
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class PrintUtilities implements Printable {
  private Component componentToBePrinted;

  public static void printComponent(Component c) {
    new PrintUtilities(c).print();
  }
  
  public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
  }
  
  public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    if (printJob.printDialog())
      try {
        printJob.print();
      } catch(PrinterException pe) {
        System.out.println("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());
      disableDoubleBuffering(componentToBePrinted);
      componentToBePrinted.paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
    }
  }

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

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



Is This A Good Question/Topic? 0
  • +

Replies To: Java Printing

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Printing

Posted 15 April 2008 - 08:16 PM

Just like to know why disabling and enabling doubleBuffering. Thanks
Was This Post Helpful? 0
  • +
  • -

#3 sErvEr79   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-May 09

Re: Java Printing

Posted 28 June 2009 - 11:24 PM

can you give an example, what component is?? I'm always get this error:
Exception in thread "main" java.lang.NullPointerException
        at calendar.PrintUtilities.print(PrintUtilities.java:107)
        at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1936)
        at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1431)
        at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1247)
        at calendar.PrintUtilities.print(PrintUtilities.java:80)
        at calendar.PrintUtilities.printComponent(PrintUtilities.java:58)
        at calendar.PrintUtilities.main(PrintUtilities.java:147)

tq in advance
Was This Post Helpful? 0
  • +
  • -

#4 hawary   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-March 12

Re: Java Printing

Posted 07 June 2012 - 03:29 PM

what if i have to print a panel contains a table it doesnt print the full table what should i do???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1