How to perform printing task using java swing?

How to perform the print task using Swing?

This is a simple program to show how you can use Java Coding to give the print option to your Java Swing projects or applications.


Summary: Demonstrate use of Print using PrinterJob.

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import static java.lang.System.err;

public final class TestPrinterJob extends JFrame implements Printable {

public TestPrinterJob(String title) {

*      Draw this component on a printer, a page at a time.  This component
* may be so large it could never fit on a screen.
* @param gr the context into which the page is drawn, high res for printer.
* @param pageFormat the paper size and orientation (landscape/portrait) of
* the page being drawn.
* @param pageIndex  the zero based index of the page to be drawn.
* @return Printable.PAGE_EXISTS if the page was rendered successfully or
* Printable.NO_SUCH_PAGE if pageIndex specifies a non-existent page. This
* is how the engine knows when you are done.
public int print(Graphics gr, PageFormat pageFormat, int pageIndex) {
// Just like Component.paint. In this example we ignore hints such as landscape/portrait in pageFormat
// If you fail to return NO_SUCH_PAGE, you will get hundreds of pages
// spewed out of your printer.
if (pageIndex > 0) {
return Printable.NO_SUCH_PAGE;
// We will be called twice, once with screen transform then again with printer transform.
// access extended 2D graphics methods.
final Graphics2D g2 = (Graphics2D) gr;// save original transform
final AffineTransform origTransform = g2.getTransform();
// Pixels here are tiny, used by printer so
// transform already is scaled to make us draw many more of them.
final AffineTransform transform = origTransform;
// apply a border on left and top of equivalent of .5 inch.
// Our values are scaled into printer pixels.
transform.translate(36.0d, 36.0d);
// usual Frame.paint()
return Printable.PAGE_EXISTS;
// ————————– OTHER METHODS ————————–

*      print the entire frame, including the title bar with the close
* widget.
private void printFrame() {
// Get a PrinterJob object to manage this print.
// Note you can’t directly instantiate one since
// PrinterJob is abstract.
PrinterJob printerJob = PrinterJob.getPrinterJob();
// Print this component just the way item would appear on screen.
// The rendering of item is handled by this component’s print method,
// that works just like paint except item gets passed a printer-style
// Graphics object instead of a screen-style and draws in higher resolution.
// pop up a dialog box for the end user to fine tune the options.
if (printerJob.printDialog()) {
try {
// render the component onto the printer or print queue.
} catch (PrinterException e) {
err.println(“Error printing: ” + e);

*    standard main method to run the test.
* @param args not used
public static void main(String[] args) {
final TestPrinterJob jframe = new TestPrinterJob(“TestPrinterJob”);
final Container contentPane = jframe.getContentPane();
jframe.setSize(215, 100);
// Note that jframe.setBackground is almost useless.
// You must set the background colour of the contentPane.
contentPane.setLayout(new BorderLayout());
final JTextArea t = new JTextArea(“This is a test page.\n” + “You are welcome!.”);
contentPane.add(t, BorderLayout.NORTH);
final JButton print = new JButton(“Print”);
print.setToolTipText(“Print the page”);
print.setFont(new Font(“Dialog”, Font.BOLD, 15));
print.addActionListener(new ActionListener() {
*          user clicked save.
* @param event button push event
public void actionPerformed(ActionEvent event) {
}// end actionPerformed
contentPane.add(print, BorderLayout.SOUTH);

To Run:

1. Simply Save the file with the same name as class name followed by .java extension.

2. Compile and Execute the program