Example usage for java.awt.print PrinterJob printDialog

List of usage examples for java.awt.print PrinterJob printDialog

Introduction

In this page you can find the example usage for java.awt.print PrinterJob printDialog.

Prototype

public abstract boolean printDialog() throws HeadlessException;

Source Link

Document

Presents a dialog to the user for changing the properties of the print job.

Usage

From source file:PrintCanvas3D.java

void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    PageFormat pageFormat = printJob.defaultPage();
    pageFormat.setOrientation(PageFormat.LANDSCAPE);
    pageFormat = printJob.validatePage(pageFormat);
    printJob.setPrintable(this, pageFormat);
    if (printJob.printDialog()) {
        try {//  ww  w  .  j  ava2s.  c  o m
            printJob.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.PeakAnnotationCalibrationPanel.java

public void onPrint() {
    PrinterJob pj = theWorkspace.getPrinterJob();
    if (pj == null)
        return;//from  w  w  w.  jav  a 2  s. c o  m

    try {
        pj.setPrintable(theChartPanel);
        if (pj.printDialog())
            pj.print();
    } catch (Exception e) {
        LogUtils.report(e);
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.AnnotationReportApplet.java

public void onPrint() {
    try {//from   w  w w.  jav  a  2 s  . c o m
        PrinterJob pj = theCanvas.getWorkspace().getPrinterJob();
        if (pj != null) {
            pj.setPrintable(theCanvas);
            if (pj.printDialog())
                theCanvas.print(pj);
        }
    } catch (Exception e) {
        LogUtils.report(e);
    }
}

From source file:com.alvermont.terraj.stargen.ui.SystemFrame.java

private void printMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_printMenuItemActionPerformed
{//GEN-HEADEREND:event_printMenuItemActionPerformed

    try {//from   w  w w  .ja  v  a  2  s.c  o  m
        List<BufferedImage> images = UIUtils.getPlanetImages(this.planets);

        BufferedImage collage = UIUtils.combineImagesHorizontal(images);

        final PrinterJob printJob = PrinterJob.getPrinterJob();
        final PageFormat pf = printJob.pageDialog(printJob.defaultPage());

        printJob.setPrintable(new ImagePrinter(collage, pf), pf);

        if (printJob.printDialog()) {
            printJob.print();
        }
    } catch (Exception e) {
        log.error("Error printing", e);

        JOptionPane.showMessageDialog(this, "Error: " + e.getMessage() + "\nCheck log file for full details",
                "Error Printing", JOptionPane.ERROR_MESSAGE);
    }

}

From source file:org.gumtree.vis.core.internal.SWTChartComposite.java

/**
 * Creates a print job for the chart.//  w  w  w .j  av  a  2 s . c o  m
 */
@Override
public void createChartPrintJob() {
    final PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    pf.setOrientation(PageFormat.LANDSCAPE);
    //      PageFormat pf2 = job.pageDialog(pf);
    PageFormat pf2 = job.defaultPage();
    pf2.setOrientation(PageFormat.LANDSCAPE);
    if (pf2 != pf) {
        Printable print = new MyPrintable();
        job.setPrintable(print, pf2);
        if (job.printDialog()) {
            getDisplay().asyncExec(new Runnable() {

                public void run() {
                    try {
                        job.print();
                    } catch (PrinterException e) {
                        MessageBox messageBox = new MessageBox(getShell(), SWT.OK | SWT.ICON_ERROR);
                        messageBox.setMessage(e.getMessage());
                        messageBox.open();
                    }
                }
            });
        }
    }

    //      PrintDialog dialog = new PrintDialog(getShell());
    //      // Prompts the printer dialog to let the user select a printer.
    //      PrinterData printerData = dialog.open();
    //
    //      if (printerData == null) // the user cancels the dialog
    //         return;
    //      // Loads the printer.
    //      final Printer printer = new Printer(printerData);
    //      getDisplay().asyncExec(new Runnable(){
    //
    //         public void run() {
    //            print(printer);
    //            printer.dispose();            
    //         }});
}

From source file:JavaWorldPrintExample2.java

/**
 * Constructor: Example2/*from w w  w.  j  av a 2s .  c o  m*/
 * <p>
 *  
 */
public JavaWorldPrintExample2() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }
}

From source file:JuliaSet2.java

public void print() {
    // Java 1.1 used java.awt.PrintJob.
    // In Java 1.2 we use java.awt.print.PrinterJob
    PrinterJob job = PrinterJob.getPrinterJob();

    // Alter the default page settings to request landscape mode
    PageFormat page = job.defaultPage();
    page.setOrientation(PageFormat.LANDSCAPE); // landscape by default

    // Tell the PrinterJob what Printable object we want to print.
    // PrintableComponent is defined as an inner class below
    String title = "Julia set for c={" + cx + "," + cy + "}";
    Printable printable = new PrintableComponent(this, title);
    job.setPrintable(printable, page);/*  www  . j  a va 2 s. com*/

    // Call the printDialog() method to give the user a chance to alter
    // the printing attributes or to cancel the printing request.
    if (job.printDialog()) {
        // If we get here, then the user did not cancel the print job
        // So start printing, displaying a dialog for errors.
        try {
            job.print();
        } catch (PrinterException e) {
            JOptionPane.showMessageDialog(this, e.toString(), "PrinterException", JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:JavaWorldPrintExample4.java

/**
 * Constructor: Example4/*from  w w  w  .  java2s.  c o m*/
 * <p>
 *  
 */
public JavaWorldPrintExample4() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }
}

From source file:JavaWorldPrintExample3.java

/**
 * Constructor: Example3/* w  ww  .j  a v a 2 s.c o m*/
 * <p>
 *  
 */
public JavaWorldPrintExample3() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Add a third page using the same painter
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }

}

From source file:com.sshtools.sshterm.SshTermSessionPanel.java

/**
 *
 *///  ww w  .  j a  v a  2  s . c om
public void printScreen() {
    try {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(terminal, pageFormat);

        if (job.printDialog()) {
            setCursor(Cursor.getPredefinedCursor(3));
            job.print();
            setCursor(Cursor.getPredefinedCursor(0));
        }
    } catch (PrinterException pe) {
        JOptionPane.showMessageDialog(this, pe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}