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

The following examples show how to use java.awt.print.PrinterJob#printDialog() . 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: 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 2
Source File: WrongPaperForBookPrintingTest.java    From openjdk-jdk8u with GNU General Public License v2.0 7 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);

    Book pageable = new Book();
    pageable.append(new WrongPaperForBookPrintingTest(), pf);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(pageable);
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 3
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 4
Source File: Print.java    From FancyBing with GNU General Public License v3.0 7 votes vote down vote up
public static void run(Component parent, Printable printable,
                       MessageDialogs messageDialogs)
{
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);
    if (! job.printDialog())
        return;
    try
    {
        job.print();
    }
    catch (Exception e)
    {
        messageDialogs.showError(parent, i18n("MSG_PRINT_FAIL"), "");
    }
}
 
Example 5
Source File: bug8023392.java    From jdk8u-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 6
Source File: bug8023392.java    From jdk8u-dev-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 7
Source File: bug8023392.java    From openjdk-8-source 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 8
Source File: PrintDialog.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // instruction dialog
    Frame instruction = new Frame("Verify that no native print dialog is showed");
    instruction.add(new TextArea(instructions));
    instruction.pack();
    instruction.show();
    // test begin
    PrintServiceStub service = new PrintServiceStub("test");
    PrintServiceLookup.registerService(service);
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintService(service);
    job.printDialog();
    System.out.println("test passed");
}
 
Example 9
Source File: DlgAttrsBug.java    From openjdk-jdk8u 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: PrintDialog.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // instruction dialog
    Frame instruction = new Frame("Verify that no native print dialog is showed");
    instruction.add(new TextArea(instructions));
    instruction.pack();
    instruction.show();
    // test begin
    PrintServiceStub service = new PrintServiceStub("test");
    PrintServiceLookup.registerService(service);
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintService(service);
    job.printDialog();
    System.out.println("test passed");
}
 
Example 11
Source File: PrintDialog.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // instruction dialog
    Frame instruction = new Frame("Verify that no native print dialog is showed");
    instruction.add(new TextArea(instructions));
    instruction.pack();
    instruction.show();
    // test begin
    PrintServiceStub service = new PrintServiceStub("test");
    PrintServiceLookup.registerService(service);
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintService(service);
    job.printDialog();
    System.out.println("test passed");
}
 
Example 12
Source File: PageDlgStackOverflowTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
    PrinterJob job = PrinterJob.getPrinterJob();
    if (job == null) {
        return;
    }
    PrintRequestAttributeSet pSet =
         new HashPrintRequestAttributeSet();
    pSet.add(DialogTypeSelection.NATIVE);
    job.printDialog(pSet);
    try {
        job.pageDialog(pSet);
    } catch (StackOverflowError e) {
        throw new RuntimeException("StackOverflowError is thrown");
    }
}
 
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: PlotBox.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void actionPerformed(ActionEvent event) {
    if (event.getSource() == _fillButton) {
        fillPlot();
    }
    else if (event.getSource() == _printButton) {
        PrinterJob job = PrinterJob.getPrinterJob();
        // [email protected]: Get the Page Format and use it.
        PageFormat format = job.pageDialog( job.defaultPage() );
        job.setPrintable(PlotBox.this, format);
        if (job.printDialog()) {
            try {
                job.print();
            } 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 15
Source File: PrintDialog.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // instruction dialog
    Frame instruction = new Frame("Verify that no native print dialog is showed");
    instruction.add(new TextArea(instructions));
    instruction.pack();
    instruction.show();
    // test begin
    PrintServiceStub service = new PrintServiceStub("test");
    PrintServiceLookup.registerService(service);
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintService(service);
    job.printDialog();
    System.out.println("test passed");
}
 
Example 16
Source File: PrintLatinCJKTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    try {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(testInstance);
        if (job.printDialog()) {
            job.print();
        }
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}
 
Example 17
Source File: bug8023392.java    From openjdk-8 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 18
Source File: ImageExportUtils.java    From chipster with MIT License 5 votes vote down vote up
public static void printComponent(Printable component) throws PrinterException {
	PrinterJob job = PrinterJob.getPrinterJob();
	job.setPrintable(component);
	
	boolean doPrint = job.printDialog();
	if (doPrint) {
		job.print();
	}
}
 
Example 19
Source File: EditorActions.java    From blog-codes with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
public void actionPerformed(ActionEvent e)
{
	if (e.getSource() instanceof mxGraphComponent)
	{
		mxGraphComponent graphComponent = (mxGraphComponent) e
				.getSource();
		PrinterJob pj = PrinterJob.getPrinterJob();

		if (pj.printDialog())
		{
			PageFormat pf = graphComponent.getPageFormat();
			Paper paper = new Paper();
			double margin = 36;
			paper.setImageableArea(margin, margin, paper.getWidth()
					- margin * 2, paper.getHeight() - margin * 2);
			pf.setPaper(paper);
			pj.setPrintable(graphComponent, pf);

			try
			{
				pj.print();
			}
			catch (PrinterException e2)
			{
				System.out.println(e2);
			}
		}
	}
}
 
Example 20
Source File: CGraphPrinter.java    From binnavi with Apache License 2.0 4 votes vote down vote up
/**
 * Prints a graph. The user is given the opportunity to choose a number of options from a dialog
 * before.
 *
 * @param parent Parent window of created dialogs.
 * @param graph The graph to print.
 */
public static void print(final JFrame parent, final ZyGraph graph) {
  final String[] area = {"Print the visible part of the graph only", "Print the whole graph"};

  final OptionHandler printOptions = new OptionHandler("Print Options");

  printOptions.addInt("Poster rows", 1);
  printOptions.addInt("Poster columns", 1);
  printOptions.addBool("Add poster coordinates", false);
  printOptions.addEnum("Print Area", area, 1);

  final Graph2DPrinter gprinter = new Graph2DPrinter(graph.getView());

  // show custom print dialog and adopt values
  if (!printOptions.showEditor()) {
    return;
  }

  gprinter.setPosterRows(printOptions.getInt("Poster rows"));
  gprinter.setPosterColumns(printOptions.getInt("Poster columns"));
  gprinter.setPrintPosterCoords(printOptions.getBool("Add poster coordinates"));

  if (printOptions.get("Print Area").equals("Print the whole graph")) {
    gprinter.setClipType(Graph2DPrinter.CLIP_GRAPH);
  } else {
    gprinter.setClipType(Graph2DPrinter.CLIP_VIEW);
  }

  // show default print dialogs
  final PrinterJob printJob = PrinterJob.getPrinterJob();
  PageFormat pageFormat = printJob.defaultPage();
  final PageFormat pageFormat2 = printJob.pageDialog(pageFormat);

  if (pageFormat2 == pageFormat) {
    return;
  }

  pageFormat = pageFormat2;

  // setup printjob.
  // Graph2DPrinter is of type Printable
  printJob.setPrintable(gprinter, pageFormat);

  if (printJob.printDialog()) {
    try {
      printJob.print();
    } catch (final PrinterException exception) {
      final String innerMessage = "E00119: " + "Graph could not be printed";
      final String innerDescription = CUtilityFunctions.createDescription(String.format(
          "The graph '%s' could not be printed because there was a problem with the printer.",
          graph.getRawView().getName()), new String[] {"There was a problem with the printer."},
          new String[] {"The print operation could not be completed."});

      NaviErrorDialog.show(parent, innerMessage, innerDescription, exception);
    }
  }
}