Example usage for java.awt.print PrinterException getMessage

List of usage examples for java.awt.print PrinterException getMessage

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:Main.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" },
            { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" },
            { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" },

    };//from   w  ww .  ja  v  a 2s .co  m
    final Object headers[] = { "English", "#" };

    JFrame frame = new JFrame("Table Printing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    JButton button = new JButton("Print");
    ActionListener printAction = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                table.print();
            } catch (PrinterException pe) {
                System.err.println("Error printing: " + pe.getMessage());
            }
        }
    };
    button.addActionListener(printAction);
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" },
            { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" },
            { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" },

    };//from  ww w . j ava 2 s .c om
    final Object headers[] = { "English", "#" };

    JFrame frame = new JFrame("Table Printing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    JButton button = new JButton("Print");
    ActionListener printAction = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                table.print(JTable.PrintMode.NORMAL);
            } catch (PrinterException pe) {
                System.err.println("Error printing: " + pe.getMessage());
            }
        }
    };
    button.addActionListener(printAction);
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TablePrintMessageFormat.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" },
            { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" },
            { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" },

    };/*from w  w w.  ja va2 s.co m*/
    final Object headers[] = { "English", "#" };

    JFrame frame = new JFrame("Table Printing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    JButton button = new JButton("Print");
    ActionListener printAction = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                MessageFormat headerFormat = new MessageFormat("Page {0}");
                MessageFormat footerFormat = new MessageFormat("- {0} -");
                table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
            } catch (PrinterException pe) {
                System.err.println("Error printing: " + pe.getMessage());
            }
        }
    };
    button.addActionListener(printAction);
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TablePrintLastVersion.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" },
            { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" },
            { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" },

    };/*from   w w w  .j  a v a  2  s .c om*/
    final Object headers[] = { "English", "#" };

    JFrame frame = new JFrame("Table Printing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    JButton button = new JButton("Print");
    ActionListener printAction = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                MessageFormat headerFormat = new MessageFormat("Page {0}");
                MessageFormat footerFormat = new MessageFormat("- {0} -");
                table.print(JTable.PrintMode.NORMAL, headerFormat, footerFormat, true,
                        new HashPrintRequestAttributeSet(), true);

            } catch (PrinterException pe) {
                System.err.println("Error printing: " + pe.getMessage());
            }
        }
    };
    button.addActionListener(printAction);
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

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);//from ww w  .j a  v a  2s .c o m
        if (!job.printDialog())
            return;
        try {
            job.print();
        } catch (PrinterException e) {
            e.printStackTrace();
            MZmineGUI.displayMessage("Error printing: " + e.getMessage());
        }
    });
}

From source file:components.TablePrintDemo.java

public void actionPerformed(java.awt.event.ActionEvent ignore) {
    MessageFormat header = new MessageFormat("Page {0,number,integer}");
    try {/* w  w  w .ja v a 2 s.c o m*/
        table.print(JTable.PrintMode.FIT_WIDTH, header, null);
    } catch (java.awt.print.PrinterException e) {
        System.err.format("Cannot print %s%n", e.getMessage());
    }
}

From source file:org.sanjose.util.JRPrinterAWT.java

/**
 *
 *//*from   w  w w . j av a  2 s .  com*/
private boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog,
        PrintService pService) 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);

    try {
        printJob.setPrintService(pService);
    } catch (PrinterException e) {
        e.printStackTrace();
        throw new JRException(e.getMessage());
    }

    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:misc.TablePrintDemo1.java

/**
 * Print the grades table./* ww  w .  j  ava 2s .c  o m*/
 */
private void printGradesTable() {
    /* Fetch printing properties from the GUI components */

    MessageFormat header = null;

    /* if we should print a header */
    if (headerBox.isSelected()) {
        /* create a MessageFormat around the header text */
        header = new MessageFormat(headerField.getText());
    }

    MessageFormat footer = null;

    /* if we should print a footer */
    if (footerBox.isSelected()) {
        /* create a MessageFormat around the footer text */
        footer = new MessageFormat(footerField.getText());
    }

    boolean fitWidth = fitWidthBox.isSelected();
    boolean showPrintDialog = showPrintDialogBox.isSelected();
    boolean interactive = interactiveBox.isSelected();

    /* determine the print mode */
    JTable.PrintMode mode = fitWidth ? JTable.PrintMode.FIT_WIDTH : JTable.PrintMode.NORMAL;

    try {
        /* print the table */
        boolean complete = gradesTable.print(mode, header, footer, showPrintDialog, null, interactive, null);

        /* if printing completes */
        if (complete) {
            /* show a success message */
            JOptionPane.showMessageDialog(this, "Printing Complete", "Printing Result",
                    JOptionPane.INFORMATION_MESSAGE);
        } else {
            /* show a message indicating that printing was cancelled */
            JOptionPane.showMessageDialog(this, "Printing Cancelled", "Printing Result",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (PrinterException pe) {
        /* Printing failed, report to the user */
        JOptionPane.showMessageDialog(this, "Printing Failed: " + pe.getMessage(), "Printing Result",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:de.dfki.owlsmx.gui.ResultVisualization.java

public void PrintGraphics2DToPS(String path) throws IOException {
    try {//ww  w . j  ava 2  s .  c  om
        //TODO implement saving to PS
        GUIState.displayWarning("Incomplete feature",
                "This feature is still under heavy development and can not yet be used");
        print2DtoPS printer = new print2DtoPS(path, chartPanel, graphPrintHeight, graphPrintHeight);
        printer.print(this.createRPPanel().getGraphics(), new PageFormat(), 0);
    } catch (PrinterException pe) {
        GUIState.displayWarning(pe.getClass().toString(), "Couldn't create PS file!\n" + pe.getMessage());
    }
}

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

/**
 * Creates a print job for the chart./*  ww w  . j a  v  a2s.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();            
    //         }});
}