Example usage for java.awt.print PrinterJob defaultPage

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

Introduction

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

Prototype

public PageFormat defaultPage() 

Source Link

Document

Creates a new PageFormat instance and sets it to a default size and orientation.

Usage

From source file:JavaWorldPrintExample4.java

/**
 * Constructor: Example4/* w  w  w  . ja va  2s .  co  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: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 {//from  w ww .  ja va2 s.  c om
            printJob.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }
}

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

/**
 * Creates a print job for the chart.//w ww  .j a  va2s.  co 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: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 ww  w .  j  a  v  a2s .c  om
        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.awt.CompositePanel.java

@Override
public void createChartPrintJob() {
    setCursor(StaticValues.WAIT_CURSOR);
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        try {/*from   w ww. j a va2  s . c om*/
            job.print();
        } catch (PrinterException e) {
            JOptionPane.showMessageDialog(this, e);
        } finally {
            setCursor(StaticValues.defaultCursor);
        }
    }
    setCursor(StaticValues.defaultCursor);
}

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

/**
 *
 *///from w  w  w.  j  ava  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:org.gvsig.remotesensing.scatterplot.chart.ScatterPlotDiagram.java

/**
 * Creates a print job for the chart./*from w ww .  ja va2  s  .c o 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.sshtools.sshterm.SshTermSessionPanel.java

/**
 *
 *
 * @param application/*from  ww w .  ja  v  a 2  s . c o m*/
 *
 * @throws SshToolsApplicationException
 */
public void init(SshToolsApplication application) throws SshToolsApplicationException {
    super.init(application);

    //  Additional connection tabs
    additionalTabs = new SshToolsConnectionTab[] { new SshTermTerminalTab() };

    //  Printing page format
    try {
        if (System.getSecurityManager() != null) {
            AccessController.checkPermission(new RuntimePermission("queuePrintJob"));
        }

        try {
            PrinterJob job = PrinterJob.getPrinterJob();

            if (job == null) {
                throw new IOException("Could not get print page format.");
            }

            pageFormat = job.defaultPage();

            if (PreferencesStore.preferenceExists(PREF_PAGE_FORMAT_ORIENTATION)) {
                pageFormat.setOrientation(
                        PreferencesStore.getInt(PREF_PAGE_FORMAT_ORIENTATION, PageFormat.LANDSCAPE));

                Paper paper = new Paper();
                paper.setImageableArea(PreferencesStore.getDouble(PREF_PAGE_FORMAT_IMAGEABLE_X, 0),
                        PreferencesStore.getDouble(PREF_PAGE_FORMAT_IMAGEABLE_Y, 0),
                        PreferencesStore.getDouble(PREF_PAGE_FORMAT_IMAGEABLE_W, 0),
                        PreferencesStore.getDouble(PREF_PAGE_FORMAT_IMAGEABLE_H, 0));
                paper.setSize(PreferencesStore.getDouble(PREF_PAGE_FORMAT_SIZE_W, 0),
                        PreferencesStore.getDouble(PREF_PAGE_FORMAT_SIZE_H, 0));
                pageFormat.setPaper(paper);
            }
        } catch (Exception e) {
            showExceptionMessage("Error", e.getMessage());
        }
    } catch (AccessControlException ace) {
        ace.printStackTrace();
    }

    enableEvents(VDU_EVENTS);

    // Set up the actions
    initActions();

    // Create the status bar
    statusBar = new StatusBar();

    dataListener = new DataNotificationListener(statusBar);

    // Create our terminal emulation object
    try {
        emulation = createEmulation();
    } catch (IOException ioe) {
        throw new SshToolsApplicationException(ioe);
    }

    emulation.addTerminalListener(this);

    // Set a scrollbar for the terminal - doesn't seem to be as simple as this
    scrollBar = new JScrollBar(JScrollBar.VERTICAL);
    emulation.setBufferSize(1000);

    // Create our swing terminal and add it to the main frame
    terminal = new TerminalPanel(emulation) {

        public void processEvent(AWTEvent evt) {
            /** We can't add a MouseWheelListener because it was not available in 1.3, so direct processing of events is necessary */
            if (evt instanceof MouseEvent && evt.getID() == 507) {
                try {
                    Method m = evt.getClass().getMethod("getWheelRotation", new Class[] {});
                    SshTermSessionPanel.this.scrollBar.setValue(SshTermSessionPanel.this.scrollBar.getValue()
                            + (SshTermSessionPanel.this.scrollBar.getUnitIncrement()
                                    * ((Integer) m.invoke(evt, new Object[] {})).intValue()
                                    * PreferencesStore.getInt(PREF_MOUSE_WHEEL_INCREMENT, 1)));

                } catch (Throwable t) {
                }
            } else {
                super.processEvent(evt);
            }
        }

        public void copyNotify() {
            copyAction.actionPerformed(null);
        }

    };
    terminal.requestFocus();
    terminal.setScrollbar(scrollBar);
    terminal.addMouseMotionListener(this);

    //terminal.addMouseWheelListener(this);
    // Center panel with terminal and scrollbar
    JPanel center = new JPanel(new BorderLayout());
    center.setBackground(Color.red);
    center.add(terminal, BorderLayout.CENTER);
    center.add(scrollBar, BorderLayout.EAST);

    // Show the context menu on mouse button 3 (right click)
    terminal.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            if ((evt.getModifiers() & MouseEvent.BUTTON3_MASK) > 0) {
                getContextMenu().setLabel(getApplication().getApplicationName());
                getContextMenu().show(terminal, evt.getX(), evt.getY());
            } else if ((evt.getModifiers() & MouseEvent.BUTTON2_MASK) > 0) {
                pasteAction.actionPerformed(null);
            }
        }
    });

    //
    //        JPanel top = new JPanel(new BorderLayout());
    //        top.add(getJMenuBar(), BorderLayout.NORTH);
    //        top.add(north, BorderLayout.SOUTH);
    setLayout(new BorderLayout());
    add(center, BorderLayout.CENTER);

    //        add(top, BorderLayout.NORTH);
    // Make sure that the swing terminal has focus
    terminal.requestFocus();
}

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

/**
 * Creates a print job for the chart./*www.  j  a  v  a 2 s  . co  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.//from  w  w  w  .j a  v a  2s . com
 */
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();
            }
        }
    }
}