2

In my Application, I have a JTable with data and I need to add a print button which adds the functionality of printing the data in a table on printer paper (send a printer job), how is that possible using the PrinterJob' class? I've been searching around for examples, but I couldn't find a simple example that prints some data from a JComponent.

This is one of the Websites I found: http://java.sun.com/products/java-media/2D/forDevelopers/sdk12print.html. But I am not sure what to focus on to understand how printing works (page format ...etc).

Thanks

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152

2 Answers2

3

I solved the problem by painting the whole table to a printer page, resizing it accordingly etc. As printing is in fact painting just to a different receiver.

@Saher please check my answer to other question where I present links which were useful for me in understanding how the API works/can be applied.

EDIT:

Please do check the tutorials especially no. 2. The way I am doing it is that I have a MyPrintUtilityclass which implements Printable interface. Its constructor takes a component I want to print. When it takes the component it calculates how many pages it will take and stores page end points. Then I have a method I call for it that initailizes the print dialog.

    public void print()
    {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        if(printJobName != null)
            printJob.setJobName(printJobName);
        printJob.setPrintable(this);    
        if(printJob.printDialog())
            try
            {
                //for faster printing, turn off double buffering
                disableDoubleBuffering(componentToPrint);
                System.out.println("Calling PrintJob.print()");
                printJob.print(new HashPrintRequestAttributeSet());
                System.out.println("End PrintJob.print()");
            }catch(PrinterException pe)
            {
                System.out.println("Error printing: " + pe);
            }finally//whatever happend (exception or not) turn back on the double buffering
            {
                enableDoubleBuffering(componentToPrint);
            }
    }

In the public int print(Graphics g, PageFormat pf, int pageIndex) method of the Printable interface I translate the graphic accordingly clipping the table for each page.

More resources to check:

http://www.java2s.com/Code/Java/2D-Graphics-GUI/PrintinJavaMultipage.htm --- printing on many pages

http://download.oracle.com/javase/tutorial/2d/printing/gui.html --- printing GUI components.

http://www.sideofsoftware.com/print_preview_tutorial.htm --- print preview tutorial

http://www.java-tips.org/java-se-tips/java.awt.print/print-the-text-file-and-print-preview-them.html --- a fantastic example of how to preview and print text files.

EDIT2:

You must seriously read this one. Here author explains how splitting on many pages works and how to translate the graphics and move the clip etc.

Community
  • 1
  • 1
Boro
  • 7,913
  • 4
  • 43
  • 85
  • how did you paint a table to the printer page? – Saher Ahwal May 26 '11 at 10:54
  • @Saher please check my edit, where I describe my approach. Plus I ma giving you more useful resources on the topic. You must try understand it yourself, otherwise I would do you a disfavour, if I was to give you a my ready code. Plus it is written in Polish, and it is old, and when you will realise you need to change it since it is not doing all you want you will struggle. I will try to find you the resource where a guy nicely explains how to approach this printing. He explains how to move the clip etc. – Boro May 26 '11 at 11:30
  • @Saher I have found the resource start with it. It is in EDIT2. – Boro May 26 '11 at 11:41
2

hmmm,

todays Java6 Oracle tutotial about printing http://download.oracle.com/javase/tutorial/2d/printing/index.html contains http://download.oracle.com/javase/7/docs/api/javax/swing/JTable.html#print%28%29 which is described in JTable Tutorial including Runnable Examples, then you have to search (on this forum too) for correct Print paginations and orientations

for example

PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
set.add(OrientationRequested.LANDSCAPE);
resultFxTable.print(JTable.PrintMode.FIT_WIDTH, header, footer, false, set, false);

note: I'm not sure that is possible correctly set Font, FontSize and FOntColor for PrintHeader and PrintFooter for JTable Printing

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • +1 nice I didn't know about his convenient method for JTable. Do you know if it also takes care of nice table printing, i.e. moving header forward and not cutting rows? – Boro May 26 '11 at 14:54