Java Code Examples for java.awt.print.PrinterJob#getPrinterJob()

The following examples show how to use java.awt.print.PrinterJob#getPrinterJob() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChartPanel.java    From buffer_bci with GNU General Public License v3.0 7 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
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);
            }
        }
    }
}
 
Example 2
Source File: ImageableAreaTest.java    From jdk8u-jdk with GNU General Public License v2.0 7 votes vote down vote up
private static void printWithJavaPrintDialog() {
    final JTable table = createAuthorTable(42);
    Printable printable = table.getPrintable(
            JTable.PrintMode.NORMAL,
            new MessageFormat("Author Table"),
            new MessageFormat("Page - {0}"));

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);

    boolean printAccepted = job.printDialog();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 3
Source File: FontPanel.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void doPrint( int i ) {
    if ( printer == null ) {
        printer = PrinterJob.getPrinterJob();
        page = printer.defaultPage();
    }
    printMode = i;
    printer.setPrintable( fc, page );

    if ( printer.printDialog() ) {
        try {
            printer.print();
        }
        catch ( Exception e ) {
            f2dt.fireChangeStatus( "ERROR: Printing Failed; See Stack Trace", true );
        }
    }
}
 
Example 4
Source File: ChartPanel.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
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);
            }
        }
    }
}
 
Example 5
Source File: ChartPanel.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
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);
            }
        }
    }
}
 
Example 6
Source File: WrongPaperPrintingTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf));
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 7
Source File: ImageableAreaTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static void printWithJavaPrintDialog() {
    final JTable table = createAuthorTable(42);
    Printable printable = table.getPrintable(
            JTable.PrintMode.NORMAL,
            new MessageFormat("Author Table"),
            new MessageFormat("Page - {0}"));

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);

    boolean printAccepted = job.printDialog();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 8
Source File: Config.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public boolean showPageSetup() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat oldFormat = getPageFormat();
    PageFormat newFormat = job.pageDialog(oldFormat);

    if (oldFormat == newFormat) {
        return false;
    }
    myPageFormat = newFormat;

    // save
    set(PAGE_ORIENTATION, myPageFormat.getOrientation());
    Paper paper = myPageFormat.getPaper();

    set(PAPER_WIDTH, paper.getWidth());
    set(PAPER_HEIGHT, paper.getHeight());

    set(AREA_X, paper.getImageableX());
    set(AREA_Y, paper.getImageableY());

    set(AREA_WIDTH, paper.getImageableWidth());
    set(AREA_HEIGHT, paper.getImageableHeight());

    return true;
}
 
Example 9
Source File: DlgAttrsBug.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void printTest() {
    PrinterJob job = PrinterJob.getPrinterJob();
    if (job.getPrintService() == null) {
        System.out.println("No printers. Test cannot continue");
        return;
    }
    job.setPrintable(new DlgAttrsBug());
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new Copies(5));
    aset.add(new PageRanges(3,4));
    aset.add(DialogTypeSelection.NATIVE);
    job.printDialog(aset);
}
 
Example 10
Source File: bug8023392.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }
}
 
Example 11
Source File: TestUnsupportedResolution.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void printWorks(String[] args)
{
    PrinterJob job=PrinterJob.getPrinterJob();
    job.setPrintable(this);
    PrintRequestAttributeSet settings=new HashPrintRequestAttributeSet();
    PrinterResolution pr = new PrinterResolution(300, 300, ResolutionSyntax.DPI);
    if (args.length > 0 && (args[0].compareTo("600") == 0)) {
        pr = new PrinterResolution(600, 600, ResolutionSyntax.DPI);
        System.out.println("Adding 600 Dpi attribute");
    } else {
        System.out.println("Adding 300 Dpi attribute");
    }
    PrintService ps = job.getPrintService();
    boolean resolutionSupported = ps.isAttributeValueSupported(pr, null, null);
    System.out.println("Is "+pr+" supported by "+ps+"?    "+resolutionSupported);
    if (resolutionSupported) {
        System.out.println("Resolution is supported.\nTest is not applicable, PASSED");
    }
    settings.add(pr);
    if (args.length > 0 && (args[0].equalsIgnoreCase("fidelity"))) {
        settings.add(Fidelity.FIDELITY_TRUE);
        System.out.println("Adding Fidelity.FIDELITY_TRUE attribute");
   }

   if (job.printDialog(settings))
   {
        try {
            job.print(settings);
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }
}
 
Example 12
Source File: PlotBox.java    From opt4j with MIT License 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent event) {
	if (event.getSource() == _fillButton) {
		fillPlot();
	} else if (event.getSource() == _printButton) {
		// FIXME: Code duplication with PlotFrame._printCrossPlatform
		PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
		PrinterJob job = PrinterJob.getPrinterJob();

		// [email protected]: Get the Page Format and use it.
		// PageFormat format = job.pageDialog(job.defaultPage());
		// job.setPrintable(PlotBox.this, format);
		job.setPrintable(PlotBox.this);

		if (job.printDialog(aset)) {
			try {
				job.print(aset);
			} catch (Exception ex) {
				Component ancestor = getTopLevelAncestor();
				JOptionPane.showMessageDialog(ancestor, "Printing failed:\n" + ex.toString(), "Print Error",
						JOptionPane.WARNING_MESSAGE);
			}
		}
	} else if (event.getSource() == _resetButton) {
		resetAxes();
	} else if (event.getSource() == _formatButton) {
		PlotFormatter fmt = new PlotFormatter(PlotBox.this);
		fmt.openModal();
	}
}
 
Example 13
Source File: bug8023392.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }
}
 
Example 14
Source File: PageSetupCommand.java    From megan-ce with GNU General Public License v3.0 5 votes vote down vote up
/**
 * parses the given command and executes it
 *
 * @param np
 * @throws java.io.IOException
 */
@Override
public void apply(NexusStreamParser np) throws Exception {
    np.matchIgnoreCase(getSyntax());
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pageFormat = job.pageDialog(new PageFormat());
    ProgramProperties.setPageFormat(pageFormat);

}
 
Example 15
Source File: TestSaveFileWithoutPrinter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    job = PrinterJob.getPrinterJob();
    if (job.getPrintService() != null) {
        System.out.println("This test requires no printers to be installed. Exiting.");
        return;
    }
    SwingUtilities.invokeLater(() -> createAndShowTestDialog());

    try {
        if (!testEndedSignal.await(testTimeout, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException(String.format(
                "Test timeout '%d ms' elapsed.", testTimeout));
        }
        if (!testPassed) {
            String failureMsg = testFailureMsg;
            if ((failureMsg != null) && (!failureMsg.trim().isEmpty())) {
                throw new RuntimeException(failureMsg);
            } else {
                throw new RuntimeException("Test failed.");
            }
        }
    } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
    } finally {
        testFinished = true;
    }
}
 
Example 16
Source File: ImageableAreaTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void printWithCustomImageareaSize() {
    final JTable table = createAuthorTable(18);
    PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
    printAttributes.add(DialogTypeSelection.NATIVE);
    printAttributes.add(new Copies(1));
    printAttributes.add(new MediaPrintableArea(
            0.25f, 0.25f, 8.0f, 5.0f, MediaPrintableArea.INCH));
    Printable printable = table.getPrintable(
            JTable.PrintMode.NORMAL,
            new MessageFormat("Author Table"),
            new MessageFormat("Page - {0}")
    );

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);

    boolean printAccepted = job.printDialog(printAttributes);
    if (printAccepted) {
        try {
            job.print(printAttributes);
            closeFrame();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        throw new RuntimeException("User cancels the printer job!");
    }
}
 
Example 17
Source File: PrintDlgSelectionAttribTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void printTest() {
    printJob = PrinterJob.getPrinterJob();
    System.out.println(" -=- Starting printing #1 -=-");
    print();
    System.out.println(" -=- Starting printing #2 -=-");
    print();
}
 
Example 18
Source File: ChatPrinter.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
 * The constructor initializes the pFormat and PJob variables.
 */
public ChatPrinter() {
    pFormat = new PageFormat();
    pJob = PrinterJob.getPrinterJob();
}
 
Example 19
Source File: PrintJob2D.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public boolean printDialog() {

        boolean proceedWithPrint = false;

        printerJob = PrinterJob.getPrinterJob();
        if (printerJob == null) {
            return false;
        }
        DialogType d = this.jobAttributes.getDialog();
        PrintService pServ = printerJob.getPrintService();
        if ((pServ == null) &&  (d == DialogType.NONE)){
            return false;
        }
        copyAttributes(pServ);

        DefaultSelectionType select =
            this.jobAttributes.getDefaultSelection();
        if (select == DefaultSelectionType.RANGE) {
            attributes.add(SunPageSelection.RANGE);
        } else if (select == DefaultSelectionType.SELECTION) {
            attributes.add(SunPageSelection.SELECTION);
        } else {
            attributes.add(SunPageSelection.ALL);
        }

        if (frame != null) {
             attributes.add(new DialogOwner(frame));
         }

        if ( d == DialogType.NONE) {
            proceedWithPrint = true;
        } else {
            if (d == DialogType.NATIVE) {
                attributes.add(DialogTypeSelection.NATIVE);
            }  else { //  (d == DialogType.COMMON)
                attributes.add(DialogTypeSelection.COMMON);
            }
            if (proceedWithPrint = printerJob.printDialog(attributes)) {
                if (pServ == null) {
                    // Windows gives an option to install a service
                    // when it detects there are no printers so
                    // we make sure we get the updated print service.
                    pServ = printerJob.getPrintService();
                    if (pServ == null) {
                        return false;
                    }
                }
                updateAttributes();
                translateOutputProps();
            }
        }

        if (proceedWithPrint) {

            JobName jname = (JobName)attributes.get(JobName.class);
            if (jname != null) {
                printerJob.setJobName(jname.toString());
            }

            pageFormat = new PageFormat();

            Media media = (Media)attributes.get(Media.class);
            MediaSize mediaSize =  null;
            if (media != null  && media instanceof MediaSizeName) {
                mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
            }

            Paper p = pageFormat.getPaper();
            if (mediaSize != null) {
                p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
                          mediaSize.getY(MediaSize.INCH)*72.0);
            }

            if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
                // AWT uses 1/4" borders by default
                p.setImageableArea(18.0, 18.0,
                                   p.getWidth()-36.0,
                                   p.getHeight()-36.0);
            } else {
                p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
            }

            pageFormat.setPaper(p);

            OrientationRequested orient =
               (OrientationRequested)attributes.get(OrientationRequested.class);
            if (orient!= null &&
                orient == OrientationRequested.REVERSE_LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
            } else if (orient == OrientationRequested.LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.LANDSCAPE);
            } else {
                pageFormat.setOrientation(PageFormat.PORTRAIT);
                }

            printerJob.setPrintable(this, pageFormat);

        }

        return proceedWithPrint;
    }
 
Example 20
Source File: PageSetupAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public synchronized void performAction() {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PrintPreferences.setPageFormat(pj.pageDialog(PrintPreferences.getPageFormat(pj)));
}