Printing problem: Looks correct but no text gets printed.

Hi,

I am making a small GUI which at some point should print some text. But this never happens. I have supplied some parts of the code below, please let me know if you can find any errors, I have been looking at this for some time now and cannot get anywhere. Maybe I am doing it all wrong (please let me know if that's the case!).

In my main program:

if (actionevent.getActionCommand().equals("Print")){

PrintRequestAttributeSet aset =new HashPrintRequestAttributeSet();

// aset.add(OrientationRequested.PORTRAIT);

// aset.add(new Copies(1));

// aset.add(new JobName("My Job", null));

PrinterJob pj = PrinterJob.getPrinterJob();

// pj.setPrintable(this);

Book book =new Book();

PageFormat portrait =new PageFormat();

portrait.setOrientation(PageFormat.PORTRAIT);

String text = textarea.getText();

int pages = textarea.getText().split("[\n]").length/58+1;

System.out.println("THERE ARE: " + pages +"PAGES IN DOCUMENT.");

for (int i=0; i<pages; i++){

book.append(new makeGraphics(text, i), portrait);

System.out.println("JUST APPENDED PAGE " + i +" TO THE BOOK.");

}

System.out.println("THE BOOK CONTAINS " + book.getNumberOfPages() +" PAGES!");

pj.setPageable(book);

PrintService[] services = PrinterJob.lookupPrintServices();

if (services.length > 0){

System.out.println("Selected printer: " + services[0].getName());

try{

pj.setPrintService(services[0]);

if (pj.printDialog(aset))

pj.print();

}

catch (PrinterException pe){

System.err.println(pe);

}

}

else

System.out.println("No printers available");

}// end if (actionevent: print)

}

So it calls the makeGraphics() class which looks like:

class makeGraphicsimplements Printable{

publicint currentPage=0;

Font font =new Font("Monospaced",Font.PLAIN,12);

String[] tokens;

public makeGraphics(String text,int page){

currentPage=page;

tokens = text.split("[\n]");

}

publicint print(Graphics g, PageFormat pf,int PageIndex){

System.out.println("METHOD JUST CALLED BY PJ.PRINT()..." + currentPage);

if (PageIndex >= 0){

Graphics2D g2d = (Graphics2D)g;

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

g2d.setColor(Color.black);

g2d.setFont(font);

int lineIndex=0;

for (int i=58*currentPage; i<58*(currentPage+1); i++){

if (i<tokens.length){

g2d.drawString(tokens[i], 8, 10+12*lineIndex);

System.out.println("Now printing: " + tokens[i]);

}

lineIndex++;

}

return Printable.PAGE_EXISTS;

}

else{

System.out.println("THAT PAGE DOES NOT EXIST...");

return Printable.NO_SUCH_PAGE;

}

}

}

Please advise/suggest.>

[5155 byte] By [mbha] at [2007-10-2 17:54:10]
# 1
here's a working example http://forum.java.sun.com/thread.jspa?threadID=614808&tstart=0
Michael_Dunna at 2007-7-13 19:12:39 > top of Java-index,Security,Event Handling...
# 2
Hi Gaurav Herei want to print some text in applets. Will you please help me as i am .NET developer and just caught first time in java.
GAURAVa at 2007-7-13 19:12:39 > top of Java-index,Security,Event Handling...