Hi,

This code done by Isaac Elias will print a JTable on multiple pages, I'm not
sure if it can handle coloros though.

Regards
/David


public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws
PrinterException {
     //if(DEBUG)System.out.println("table print--"+pageIndex);
     //----------//

     Graphics2D g2 = (Graphics2D) g;
     Font printFont = new Font("Helvetica", Font.PLAIN, 10);
     setEnabled(false);
     g2.setFont(printFont);
     g2.setColor(Color.black);
     //paint(g2);//paint table

     int fontHeight=g2.getFontMetrics().getHeight();
     int fontDesent=g2.getFontMetrics().getDescent();
     int fontWidth=g2.getFontMetrics().getMaxAdvance();
     //----
     int placeForDel=2;
     double pageHeight =
pageFormat.getImageableHeight()-fontHeight-placeForDel;//leave room fot note
     double pageWidth = pageFormat.getImageableWidth();
     double tableWidth =(double) getColumnModel().getTotalColumnWidth();
     //-----
     double scale = 1; //both x and y
     if (tableWidth >= pageWidth)//only scale if the width is to big
      scale =  pageWidth / tableWidth;

     //----
     double headerHeightOnPage=tableHeader.getHeight()*scale;
     //double headerWidth=tableHeader.getWidth()*scale;
     double tableWidthOnPage=tableWidth*scale;
     //-----
     double oneRowHeight=(rowHeight+rowMargin)*scale;
     int numRowsOnAPage=(int)((pageHeight-headerHeightOnPage)/oneRowHeight);
     double pageHeightForTable=oneRowHeight*numRowsOnAPage;
     int
totalNumPages=(int)Math.ceil(((double)getRowCount())/numRowsOnAPage);
     if(pageIndex>=totalNumPages)
      return NO_SUCH_PAGE;
     file://-----
     this.clearSelection();
     this.requestFocus();
     file://-----------
     // Format the current time. SimpleDateFormat formatter
     java.text.SimpleDateFormat formatter= new java.text.SimpleDateFormat
("MMM d, yyyy 'at' hh:mm aaa");
      Date currentTime = new Date();
     String dateString = formatter.format(currentTime);
     //------------
     g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
     //delimitter down

g2.drawString("VITS",7,(int)(pageHeight+fontHeight-fontDesent+placeForDel));
//bottom right

g2.drawString(dateString,(int)pageWidth/2-5*fontWidth,(int)(pageHeight+fontH
eight-fontDesent+placeForDel));//bottom right
     g2.drawString("Page:
"+(pageIndex+1),(int)pageWidth-3*fontWidth,(int)(pageHeight+fontHeight-fontD
esent+placeForDel));//bottom right
     g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage),
(int)Math.ceil(headerHeightOnPage));
     g2.scale(scale,scale);
     tableHeader.paint(g2);//paint header at top
     g2.scale(1/scale,1/scale);
     g2.translate(0f,headerHeightOnPage-pageIndex*pageHeightForTable);
     g2.setClip(0, (int)(pageHeightForTable*pageIndex),(int)
Math.ceil(tableWidthOnPage),(int) Math.ceil(pageHeightForTable));//PENDING!
last page
     g2.scale(scale,scale);
     paint(g2);//paint table
     g2.scale(1/scale,1/scale);//fix delimitter
     g2.translate(0f,pageIndex*pageHeightForTable-headerHeightOnPage);
     g2.setClip(0, 0,(int) Math.ceil(pageWidth),
(int)Math.ceil(pageFormat.getImageableHeight()));
     g2.setStroke(new BasicStroke(2));

g2.drawLine(0,(int)pageHeight+2,(int)pageWidth,(int)pageHeight+2);//delimitt
er
     return Printable.PAGE_EXISTS;
    }

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to