Results 1 to 2 of 2
  1. #1
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default Please help in prining JPanel

    Hey friends,
    I want to print a JPanel along with its component for which i have my PrintUtilities class as I attached it.

    Here the problem is that whenever i want to print the jPanel i have to passed it through parameter of printComponent(Component) method and it shows me the print dialog.

    What i actually want is to show the printDialog only once in running application for printing setup and after that it directly prints the component whenever i call the printComponent() method.

    Please suggest me what i have to change in my code???

    Please help me...
    Attached Files Attached Files

  2. #2
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Here is the same code what i have attached before:

    package Classes;

    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);
    }
    }

    @Override
    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);
    }
    }

Similar Threads

  1. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  2. Adding Jpanel ontop of another Jpanel
    By Manfizy in forum AWT / Swing
    Replies: 4
    Last Post: 03-05-2011, 10:34 PM
  3. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 06:00 PM
  4. Replies: 1
    Last Post: 11-09-2010, 09:28 AM
  5. .add to a JPanel
    By harrier in forum NetBeans
    Replies: 11
    Last Post: 07-13-2010, 10:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •