Hello all,

I am having trouble printing I have spent just too long with this task now & I need to resolve it...therefore I was hoping that someone here could maybe help me?

The class is requested here:



if (event.getSource() == print)
{
printMenu = new printUtility(this.getGraphics(),theResult);
}
if (event.getSource() == newProfessional)



And then attempts to print the contents of the passed text area as below, the problem is that it starts up the dprint dialog :-) & even starts the printer :-) but sadly prints nothing :-(
Could anyone help me discover what I am doing wrong please?


import java.awt.*;
import java.awt.print.*;

public class printUtility implements Printable {
private Component componentToBePrinted; //holds the component for printing
private PrinterJob printJob;
Graphics printGraphics;

public printUtility(Graphics g, Component textArea)
{
printJob = PrinterJob.getPrinterJob();
printGraphics=g;
componentToBePrinted=textArea;
printJob.setPrintable(this);

if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}

//print method of printable interface with arguments:
//graphics(the context that is used to write to the printer)
//pageformat(provides details of the page size, position & size of the printable area on screen & orientation)
//pageindex(carries an index value for the page 1stpage = 0, 2nd = 1 etc)
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
if (pageIndex > 0)
{
return NO_SUCH_PAGE; //only want 2 print 1 page so if more return
} //NO_SUCH_PAGE to stop printing
else
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.black);
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //translate method moves the user
componentToBePrinted.paint(g2d); //coordinates so that 0,0 is
return PAGE_EXISTS; //positioned @ the top left corner
} //of the printable page
}
}