Printing Second Page(again)
First Thanks to T Jacobs and Gita_Weiner for your very helpful replies.
The PageIndex thing still confounds me.
I have a function that returns the number of pages to print and now I don't return NO_SUCH_PAGE unless PageIndex is greater than the number of pages.
My code now looks like this
import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import javax.swing.JComponent;
import java.util.List;
import java.util.Vector;
import java.util.ArrayList;
publicclass SchedulePrintimplements Printable, Pageable{
private List textList =null;
private Font sfont =new Font("Serif", Font.PLAIN,14);
privateint NumberOfPages=-1;
privateint startpos = 0;
privateint c = 96;
privateint r = 100;
private PageFormat mFormat;
public SchedulePrint(List inputList,int numpages){
textList = inputList;
NumberOfPages=numpages;//Number of pages is calculated by the main class
}// which instantiates this class, sending the number of
// pages as a parameter to the constructor.
publicint print(Graphics g,PageFormat Pf,int pageIndex)
throws PrinterException{
if (pageIndex > getNumberOfPages()){
return Printable.NO_SUCH_PAGE;
}
mFormat = Pf;
Graphics2D g2 = (Graphics2D)g;
g2.setFont(sfont);
g2.setPaint(Color.black);
r = 100;
System.out.println("startpos= "+startpos+" pageIndex= "+pageIndex);
for(int k=startpos;k<textList.size();k++){
String t = (String)textList.get(k);
if(r>=730)//This is where I want to force the printer
{// to print a new page
r=100;// but it doesn't work
startpos = k;
//pageIndex++;
return PAGE_EXISTS;// It just prints the second page twice
//break; //I tried to break out of the for loop and let it fall though
// to the return PAGE_EXISTS; statement at the end but it's no better.
}
g2.drawString(t,c,r);
r+=14;
System.out.println("Printing..."+t+" r= "+r+" k= "+k);
}
return PAGE_EXISTS;
}
publicint getNumberOfPages(){
return NumberOfPages;
}
public Printable getPrintable(int i){
returnthis;
}
public PageFormat getPageFormat(int page){
if (mFormat ==null){
PrinterJob job = PrinterJob.getPrinterJob();
mFormat = job.defaultPage();
}
return mFormat;
}
publicvoid run(){
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(new SchedulePrint(textList,NumberOfPages));
job.setPageable(this);
if(job.printDialog()){
try{
job.print();
}catch (PrinterException e){}
}
}
}
When I look at my trace statements it appears that the PageIndex is not
updated the first time around.
And so the second page is now printed twice!!
What is happening here?
Also it appears that returning PAGE_EXISTS does not automatically
cause the second page to be printed.
I'm sure I am missing something here, but what could it be?

