Printing in Java is a pretty complex thing, and I will try to introduce you to the basics by creating a small application that will print Hello World on a blank page. After this tutorial, you will be able to print a lot of things if you master the basics. You must have the basics of Java as well as GUI notions to be able to follow. Let's get started.

This is it for the GUI creation. If you run the program, all you will see will be a window with a button in it that does absolutely nothing when clicked. Let's make it do something (hopefully printing)!

Adding the button's action

If your ActionListener stuff is a little rusty, I'll refresh it for you: we will add the button's action by using:

btnPrint.addActionListener(new btnPrintAction());

In this example, btnPrintAction must be an ActionListener. We will achieve this by implementing ActionListener to the btnPrintAction class we will create:

public static class btnPrintAction implements ActionListener{
}

Now, if you create this class in your main class, you will get an error message similar to:
The class btnPrintAction must implement the abstract method ActionListener.actionPerformed (ActionEvent)

The actionPerformed method is called when you click the button. Make sure you understand this before you proceed, even if it is not the main purpose of this tutorial.

Implementing the Printable interface

To be able to get something out of your printer, you need a class which implements the Printable interface. Remember: a class can only extend one class, but can implement multiple ones! What I mean: implement both ActionListener and Printable to the class you just created, and import java.awt.print.*.

At first glance, the print method used here may seem complicated with its arguments. Don't worry, they are not all used and are quite simple:

Graphics gx: A graphics object in which we wil add the different element to make a nice page to print. We can add text, images, and everything we want out of our printer.
PageFormat pf: The page format, specified by the Print dialog we will pop later.
int page: The number of the page we are printing. The first page has the index 0.

Feel free to explore the methods of the objects specified above. This is it, our class is created. Now let's add some real action!

Popping the print dialog

When you click the button, a print dialog should pop. Trust me, you absolutely need this one. Put that in the actionPerformed method:

Now, this code will pop a dialog (the one that you always see when you attempt to print a document) asking for many parameters, and will print when you click OK. We are now ready to actually configure what will be printed.

Setting the page layout

Although the title may seem to introduce a complex part, don't get fooled. We will only tell the printer to print "Hello World" on the page (5 lines needed). This is in the print method. One important thing: the units used are not pixels! They are points, and there are 72 points in an inch.

if (page>0){return NO_SUCH_PAGE;}

Since we need only one page, if we attempt to print a second page (remember page indexes start at zero), this will prevent multiple page printing.

Graphics2D g = (Graphics2D)gx;

This casts the Graphics object gx to a Graphics2D object g, which we will print.

g.translate(pf.getImageableX(), pf.getImageableY());

This modifies the Graphics2D object to make its origin (0, 0) match the corner of the imageable area (the area of the page we can print on) so we don't print for nothing in an area.

g.drawString ("Hello world", 100, 100);

This draws the Hello World string to the point (100, 100) of the page's imageable area.

return PAGE_EXISTS;

The page exists, it is the index 0 (it can't be greaten than 0, because the first line would have blocked it. Now here's the full code:

what if i print a Jpanel that contain a Jlabels, Jtextfields, and Jtable
the page is printed but the problem is the Jtable not fully printed if it have alot of rows and the scroll is running
what should i do?