Reputation: 9237
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
Upvotes: 2
Views: 15653
Reputation: 109815
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
Upvotes: 2
Reputation: 7943
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.
Upvotes: 3