Example usage for java.awt.print PrinterJob setPrintable

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

Introduction

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

Prototype

public abstract void setPrintable(Printable painter, PageFormat format);

Source Link

Document

Calls painter to render the pages in the specified format .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    PrinterJob pjob = PrinterJob.getPrinterJob();
    PageFormat pf = pjob.defaultPage();
    pjob.setPrintable(null, pf);

    if (pjob.printDialog()) {
        pjob.print();/* w  w  w .ja  v a 2  s.c o  m*/
    }

}

From source file:BasicPrint.java

public static void main(String[] args) {
    PrinterJob pjob = PrinterJob.getPrinterJob();
    PageFormat pf = pjob.defaultPage();
    pjob.setPrintable(new BasicPrint(), pf);
    try {/*from  w  w w  . j a  va  2s .  c  o  m*/
        pjob.print();
    } catch (PrinterException e) {
    }
}

From source file:Main.java

public static void main(String args[]) {

    try {//from   w  w w . j a v  a 2s.  c  om
        String cn = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(cn); // Use the native L&F
    } catch (Exception cnf) {
    }
    PrinterJob job = PrinterJob.getPrinterJob();
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    PageFormat pf = job.pageDialog(aset);
    job.setPrintable(new Main(), pf);
    boolean ok = job.printDialog(aset);
    if (ok) {
        try {
            job.print(aset);
        } catch (PrinterException ex) {
            /* The job did not successfully complete */
        }
    }
    System.exit(0);
}

From source file:PrintDialogExample.java

public static void main(String args[]) {

    try {/*from  w w w . j  a v  a  2  s  .c o  m*/
        String cn = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(cn); // Use the native L&F
    } catch (Exception cnf) {
    }
    PrinterJob job = PrinterJob.getPrinterJob();
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    PageFormat pf = job.pageDialog(aset);
    job.setPrintable(new PrintDialogExample(), pf);
    boolean ok = job.printDialog(aset);
    if (ok) {
        try {
            job.print(aset);
        } catch (PrinterException ex) {
            /* The job did not successfully complete */
        }
    }
    System.exit(0);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    PrinterJob pj = PrinterJob.getPrinterJob();

    PageFormat pf = pj.defaultPage();
    Paper paper = new Paper();
    double margin = 36; // half inch
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
    pf.setPaper(paper);//  w w w  . j a  v  a 2  s. co m

    pj.setPrintable(new MyPrintable(), pf);
    if (pj.printDialog()) {
        try {
            pj.print();
        } catch (PrinterException e) {
            System.out.println(e);
        }
    }
}

From source file:io.github.mzmine.util.jfreechart.JFreeChartUtils.java

public static void printChart(ChartViewer chartNode) {

    // As of java 1.8.0_74, the JavaFX printing support seems to do poor
    // job. It creates pixelated, low-resolution print outs. For that
    // reason, we use the AWT PrinterJob class, until the JavaFX printing
    // support is improved.
    SwingUtilities.invokeLater(() -> {
        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        PageFormat pf2 = job.pageDialog(pf);
        if (pf2 == pf)
            return;
        ChartPanel p = new ChartPanel(chartNode.getChart());
        job.setPrintable(p, pf2);
        if (!job.printDialog())
            return;
        try {/*from  w w  w  .  j  a  v  a2  s  . c  om*/
            job.print();
        } catch (PrinterException e) {
            e.printStackTrace();
            MZmineGUI.displayMessage("Error printing: " + e.getMessage());
        }
    });
}

From source file:PrintableComponent.java

/**
 * This method is not part of the Printable interface.  It is a method
 * that sets up the PrinterJob and initiates the printing.
 *//* w  w w  . j  a  v a2s .  c om*/
public void print() throws PrinterException {
    // Get the PrinterJob object
    PrinterJob job = PrinterJob.getPrinterJob();
    // Get the default page format, then allow the user to modify it
    PageFormat format = job.pageDialog(job.defaultPage());
    // Tell the PrinterJob what to print
    job.setPrintable(this, format);
    // Ask the user to confirm, and then begin the printing process
    if (job.printDialog())
        job.print();
}

From source file:playground.artemc.calibration.charts.CustomChartPanel.java

public void createPrintJob() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        if (job.printDialog()) {
            try {
                job.print();/*  w w  w  . j  a va 2  s.  c  o  m*/
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(this, e);
            }
        }
    }
}

From source file:org.fhaes.fhfilechecker.FrameViewHelp.java

public void btnPrint_actionPerformed(ActionEvent e) {
    // Get a PrinterJob
    PrinterJob job = PrinterJob.getPrinterJob();
    // Ask user for page format (e.g., portrait/landscape)
    PageFormat pf = job.pageDialog(job.defaultPage());
    // Specify the Printable is an instance of
    // PrintListingPainter; also provide given PageFormat
    job.setPrintable(new PrintListingPainter(out_file_name), pf);
    // Print 1 copy
    job.setCopies(1);//from w w w  .  ja  va  2s .  co m
    // Put up the dialog box
    if (job.printDialog()) {
        // Print the job if the user didn't cancel printing
        try {
            job.print();
        } catch (Exception ex) {
            /* handle exception */}
    }
}

From source file:com.alvermont.terraj.planet.ui.TerrainFrame.java

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

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

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

    if (printJob.printDialog()) {
        try {/* ww  w.  j  a v  a2  s.com*/
            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);
        }
    }
}