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 boolean printDialog(PrintRequestAttributeSet attributes) throws HeadlessException 

Source Link

Document

A convenience method which displays a cross-platform print dialog for all services which are capable of printing 2D graphics using the Pageable interface.

Usage

From source file:Main.java

public static void main(String args[]) {

    try {/*from w  w  w.ja  v a  2s.com*/
        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 {/*w  w w.j  av a 2s .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:Test.java

public Test() {
    this.setSize(200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());

    JColorChooser.showDialog(this, null, Color.blue);
    JButton printDialogButton = new JButton("Print Dialog");
    printDialogButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            attributes.add(DialogTypeSelection.COMMON);
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.printDialog(attributes);

        }/*from w  w w  .  ja v a 2 s  . com*/
    });
    this.add(printDialogButton);
}

From source file:PrintTest.java

public PrintTestFrame() {
    setTitle("PrintTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    canvas = new PrintComponent();
    add(canvas, BorderLayout.CENTER);

    attributes = new HashPrintRequestAttributeSet();

    JPanel buttonPanel = new JPanel();
    JButton printButton = new JButton("Print");
    buttonPanel.add(printButton);/*  w  ww.  j  a  va2s  .  c  o m*/
    printButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPrintable(canvas);
                if (job.printDialog(attributes))
                    job.print(attributes);
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(PrintTestFrame.this, e);
            }
        }
    });

    JButton pageSetupButton = new JButton("Page setup");
    buttonPanel.add(pageSetupButton);
    pageSetupButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.pageDialog(attributes);
        }
    });

    add(buttonPanel, BorderLayout.NORTH);
}

From source file:BookTest.java

public BookTestFrame() {
    setTitle("BookTest");

    text = new JTextField();
    add(text, BorderLayout.NORTH);

    attributes = new HashPrintRequestAttributeSet();

    JPanel buttonPanel = new JPanel();

    JButton printButton = new JButton("Print");
    buttonPanel.add(printButton);/*from  ww w .  j  av  a 2s.  c o  m*/
    printButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPageable(makeBook());
                if (job.printDialog(attributes)) {
                    job.print(attributes);
                }
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(BookTestFrame.this, e);
            }
        }
    });

    JButton pageSetupButton = new JButton("Page setup");
    buttonPanel.add(pageSetupButton);
    pageSetupButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            PrinterJob job = PrinterJob.getPrinterJob();
            pageFormat = job.pageDialog(attributes);
        }
    });

    JButton printPreviewButton = new JButton("Print preview");
    buttonPanel.add(printPreviewButton);
    printPreviewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            PrintPreviewDialog dialog = new PrintPreviewDialog(makeBook());
            dialog.setVisible(true);
        }
    });

    add(buttonPanel, BorderLayout.SOUTH);
    pack();
}

From source file:com.aw.core.report.ReportFileGenerator.java

public String generateAndPrint(String reportName, Map params, String tempDirectory, Connection conn)
        throws JRException {
    // force temp directory end with "/"
    //        tempDirectory = formatDirectory(tempDirectory);
    //        String fullOutFileName = generateFile(conn);
    //        String fullPdfFileName = fullOutFileName + ".pdf";
    //        JasperExportManager.exportReportToPdfFile(fullOutFileName, fullPdfFileName);

    //Report loading and compilation
    //        JasperDesign jasperDesign = JRXmlLoader.load(compiledReport);
    //        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

    // - Report execution

    //        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);

    try {/*w  w w. j  a va  2  s.c  om*/
        // - Report creation to printer
        PrinterJob job = PrinterJob.getPrinterJob();
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; // This is the Flavour JasperReports uses

        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.ISO_A4); // ..or whatever the document size is

        PrintService service = null;
        if (job.printDialog(aset))
            service = job.getPrintService();

        InputStream jasperReport = new FileInputStream("C:/ProgFile/iReport-2.0.2/CorrelativoDocumento.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);

        // Export the report using the JasperPrint instance
        JRExporter exporter = new JRPrintServiceExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
                service.getAttributes());
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);

        exporter.exportReport();

        //            String fullPdfFileName = "D:\\test.pdf";
        //            JasperExportManager.exportReportToPdfFile(jasperPrint, fullPdfFileName);

        return null;
    } catch (FileNotFoundException e) {
        throw AWBusinessException.wrapUnhandledException(logger, e);
    }

}

From source file:org.martus.client.swingui.actions.ActionMenuCharts.java

private boolean printToPrinter(JFreeChart chart) throws PrinterException {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(new PrintableChart(chart));
    HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    removeJavaLogoFromTitle(attributes);
    if (!printJob.printDialog(attributes))
        return false;

    printJob.print(attributes);/* ww w  .j a va 2 s.  com*/
    return true;
}