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:org.gvsig.remotesensing.scatterplot.chart.ScatterPlotDiagram.java

/**
 * Creates a print job for the chart./*from   w  w  w.j a  v  a2 s .co m*/
 */
public void createChartPrintJob() {

    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();
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(this, e);
            }
        }
    }

}

From source file:com.floreantpos.jasperreport.engine.print.JRPrinterAWT.java

/**
 *
 *///from w  w  w .  ja v a  2  s  .  c o m
private boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog) throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
            || lastPageIndex >= jasperPrint.getPages().size()) {
        throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of "
                + jasperPrint.getPages().size());
    }

    printerName = jasperPrint.getProperty("printerName");
    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName(jasperPrint.getName());

    switch (jasperPrint.getOrientationValue()) {
    case LANDSCAPE: {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        break;
    }
    case PORTRAIT:
    default: {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
    }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
        if (withPrintDialog) {
            if (printJob.printDialog()) {
                printJob.print();
            } else {
                isOK = false;
            }
        } else {
            printJob.print();
        }
    } catch (Exception ex) {
        throw new JRException("Error printing report.", ex);
    }

    return isOK;
}

From source file:com.openbravo.pos.util.JRPrinterAWT411.java

/**
 *
 *//*from   w ww  .  j ava 2 s .com*/
private boolean printPages(int firstPageIndex, int lastPageIndex, PrintService service) throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
            || lastPageIndex >= jasperPrint.getPages().size()) {
        throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of "
                + jasperPrint.getPages().size());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName("JasperReports - " + jasperPrint.getName());

    switch (jasperPrint.getOrientationValue()) {
    case LANDSCAPE: {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        break;
    }
    case PORTRAIT:
    default: {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
    }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
        if (service == null) {
            if (printJob.printDialog()) {
                printJob.print();
            } else {
                isOK = false;
            }
        } else {
            printJob.setPrintService(service);
            printJob.print();
        }
    } catch (Exception ex) {
        throw new JRException("Error printing report.", ex);
    }

    return isOK;
}

From source file:com.openbravo.pos.util.JRPrinterAWT.java

/**
 *
 *///  www .  j  a  v  a  2s . co  m
public boolean printPages(int firstPageIndex, int lastPageIndex, PrintService service) throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
            || lastPageIndex >= jasperPrint.getPages().size()) {
        throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of "
                + jasperPrint.getPages().size());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName("JasperReports - " + jasperPrint.getName());

    switch (jasperPrint.getOrientationValue()) {
    case LANDSCAPE: {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        break;
    }
    case PORTRAIT:
    default: {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
    }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
        if (service == null) {
            if (printJob.printDialog()) {
                printJob.print();
            } else {
                isOK = false;
            }
        } else {
            printJob.setPrintService(service);
            printJob.print();
        }
    } catch (Exception ex) {
        throw new JRException("Error printing report.", ex);
    }

    return isOK;
}

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

public void printScreen() {
    try {/*  ww w  .j av  a 2 s .c o m*/
        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);
    }
}

From source file:rulebender.editors.dat.view.CustomizedChartComposite.java

/**
 * Creates a print job for the chart./* w  w  w .j  a v  a 2 s  . c o  m*/
 */
public void createChartPrintJob() {
    //FIXME try to replace swing print stuff by swt
    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();
            } catch (PrinterException e) {
                MessageBox messageBox = new MessageBox(this.canvas.getShell(), SWT.OK | SWT.ICON_ERROR);
                messageBox.setMessage(e.getMessage());
                messageBox.open();
            }
        }
    }
}

From source file:com.rcp.wbw.demo.ChartComposite.java

/**
 * Creates a print job for the chart./*  w w w .ja  va2s  .  c o  m*/
 */
public void createChartPrintJob() {
    // FIXME try to replace swing print stuff by swt
    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();
            } catch (PrinterException e) {
                MessageBox messageBox = new MessageBox(this.canvas.getShell(), SWT.OK | SWT.ICON_ERROR);
                messageBox.setMessage(e.getMessage());
                messageBox.open();
            }
        }
    }
}

From source file:org.mwc.cmap.grideditor.chart.FixedChartComposite.java

/**
 * Creates a print job for the chart.// w  w w .  j  a  v  a  2 s . c  om
 */
public void createChartPrintJob() {
    // FIXME try to replace swing print stuff by swt
    final PrinterJob job = PrinterJob.getPrinterJob();
    final PageFormat pf = job.defaultPage();
    final PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        if (job.printDialog()) {
            try {
                job.print();
            } catch (final PrinterException e) {
                final MessageBox messageBox = new MessageBox(this.canvas.getShell(), SWT.OK | SWT.ICON_ERROR);
                messageBox.setMessage(e.getMessage());
                messageBox.open();
            }
        }
    }
}

From source file:net.sf.jasperreports.engine.print.JRPrinterAWT.java

/**
 *
 */// w  w  w .  ja  va 2 s.com
public boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog) throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
            || lastPageIndex >= jasperPrint.getPages().size()) {
        throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of "
                + jasperPrint.getPages().size());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);
    if (jasperPrint.getProperty("printService") != null) {
        String printServiceName = jasperPrint.getProperty("printService");
        try {
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            for (PrintService se : services) {
                if (se.getName().contains(printServiceName)) {
                    printJob.setPrintService(se);
                    break;
                }
            }

        } catch (PrinterException ex) {
            Logger.getLogger(JRPrinterAWT.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName("JasperReports - " + jasperPrint.getName());

    switch (jasperPrint.getOrientationValue()) {
    case LANDSCAPE: {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        break;
    }
    case PORTRAIT:
    default: {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
    }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
        if (withPrintDialog) {
            if (printJob.printDialog()) {
                printJob.print();
            } else {
                isOK = false;
            }
        } else {
            printJob.print();
        }
    } catch (Exception ex) {
        throw new JRException("Error printing report.", ex);
    }

    return isOK;
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java

/**
 * @param actionableTree/*from w w w  . ja v a 2s  .c  o m*/
 */
public void print(final JTree actionableTree) {
    /*if (true)
    {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(OrientationRequested.LANDSCAPE);
    aset.add(new Copies(2));
    aset.add(new JobName("My job", null));
            
    // Create a print job 
    PrinterJob pj = PrinterJob.getPrinterJob();       
    pj.setPrintable(this);
    // locate a print service that can handle the request 
    PrintService[] services =
        PrinterJob.lookupPrintServices();
            
    if (services.length > 0) {
        System.out.println("selected printer " + services[0].getName());
        try {
            pj.setPrintService(services[0]);
            pj.pageDialog(aset);
            if(pj.printDialog(aset)) {
                pj.print(aset);
            }
        } catch (PrinterException pe) { 
            System.err.println(pe);
        }
    }
    return;
    }*/

    DefaultMutableTreeNode clonedTree = GhostActionableTree
            .makeDeepCopy((DefaultMutableTreeNode) actionableTree.getModel().getRoot());
    GhostActionableTree printTree = new GhostActionableTree(this, new DefaultTreeModel(clonedTree));
    printTree.setRowHeight(ROW_HEIGHT);
    //printTree.setEditable(false);
    //printTree.setVisibleRowCount(15);
    ContainerTreeRenderer renderer = new ContainerTreeRenderer(null, false, false);
    renderer.setBGColor(Color.WHITE);
    renderer.setFont(getFont().deriveFont(8.0f));
    //renderer.setLeafIcon(IconManager.getIcon(CollectionObject.class.getSimpleName(), IconManager.IconSize.Std32));
    //renderer.setVerticalTextPosition(SwingConstants.CENTER);
    printTree.setCellRenderer(renderer);

    for (int row = 0; row < printTree.getRowCount(); row++) {
        printTree.expandRow(row);
    }

    PrintablePanel p = new PrintablePanel(new BorderLayout(), printTree);
    p.add(printTree, BorderLayout.CENTER);

    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    //PrinterResolution        pr   = new PrinterResolution(300, 300, PrinterResolution.DPI);
    //MediaPrintableArea       mpa  = new MediaPrintableArea(8,21, 210-16, 296-42, MediaPrintableArea.MM);

    //aset.add( MediaSizeName.IS);
    //aset.add( pr );
    //aset.add( mpa );
    aset.add(new Copies(1));
    aset.add(OrientationRequested.PORTRAIT);
    aset.add(PrintQuality.HIGH);

    PrinterJob job = PrinterJob.getPrinterJob();
    /*PageFormat pageFormat = job.defaultPage();
    Paper      paper      = pageFormat.getPaper();
    paper.setSize(pageFormat.getWidth(), pageFormat.getHeight());
    paper.setImageableArea(
    0,
    0,
    pageFormat.getWidth(),
    pageFormat.getHeight()
    );
    //aset.add( Fidelity.FIDELITY_TRUE );
    pageFormat.setPaper(paper);
            
    Book book = new Book();
    book.append(p, pageFormat, 1);
    job.setPageable(book);*/

    job.setPrintable(p);
    boolean ok = job.printDialog();
    if (ok) {
        try {
            job.print(aset);

        } catch (PrinterException ex) {
            ex.printStackTrace();
            /* The job did not successfully complete */
        }
    }
}