Example usage for java.awt.print PrinterJob print

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

Introduction

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

Prototype

public void print(PrintRequestAttributeSet attributes) throws PrinterException 

Source Link

Document

Prints a set of pages using the settings in the attribute set.

Usage

From source file:Main.java

public static void main(String args[]) {

    try {//from   ww w. j a  v  a2s . 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 a2  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: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);/*from www .ja  v a2s .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  2 s .  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: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);
    return true;//from w w  w.ja v  a2s.  c  o m
}

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

/**
 * @param actionableTree/*from   w  w  w .ja v  a 2 s. c  om*/
 */
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 */
        }
    }
}