Why the method of the following class is getting invoked several times even if the condition is false.Please help me with my code...
public class PRDDayBook {
int remX;
public PRDDayBook(int x) {
remX = x;
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
Paper paper = new Paper();
double margin = 9;
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
pf.setPaper(paper);
pj.setPrintable(new MyPrintable(), pf);
if (pj.printDialog()) {
try {
pj.print();
} catch (PrinterException e) {
System.out.println(e);
}
}
}
class MyPrintable implements Printable {
public int print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex != 0) {
return NO_SUCH_PAGE;
}
int x = 100;
int y = 50;
Graphics2D g2 = (Graphics2D) g;
g2.setFont(new Font("Serif", Font.PLAIN, 10));
g2.setPaint(Color.black);
g2.drawString("S.No", x, 30);
g2.drawString("Date", x + 30, 30);
g2.drawString("Particulars", x + 100, 30);
g2.drawString("Amount Rs", x + 250, 30);
g2.drawString("Voucher No", x + 300, 30);
// for printing the items with teir rates and cost
int hor = DayBookRecords.data.size();
int ver = DayBookRecords.data.get(0).size();
double ph = pf.getImageableHeight();
double pw = pf.getImageableWidth();
int m=0;
System.out.println("HOR:" + hor + " VER " + ver);
for (int i = remX + 1; i < hor; i++) {
for (int j = 0; j < ver; j++) {
g2.drawString("" + DayBookRecords.data.get(i).get(j), x, y);
if (j == 0) {
x = x + 30;
}
if (j == 1) {
x = x + 70;
}
if (j == 2) {
x = x + 150;
}
if (j == 3 || j == 4) {
x = x + 50;
}
}
x = 100;
y = y + 15;
if (y > ph) {
m = i;
new PRDDayBook(m);
break;
}
}
Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf.getImageableWidth(), pf.getImageableHeight());
g2.draw(outline);
return PAGE_EXISTS;
}
}
}
Here i'm trying to print data to multiple pages in another way... everything works fine but the last page is printed more than one times. I cannot figure out where am i making mistake .Please Help!!