Hi Knut,
The code below maybe can be of help. This code,
which was coded by my friend Isaac Elias, prints a JTable and some
text.
Best Regards
/David Lilljegren
public int print(Graphics g, PageFormat
pageFormat, int pageIndex) throws PrinterException
{
if(DEBUG)System.out.println("table print--"+pageIndex);
//----------//
Graphics2D g2 = (Graphics2D) g;
g2.setFont(printFont);
g2.setColor(Color.black);
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;
if(DEBUG)System.out.println("table print--"+pageIndex);
//----------//
Graphics2D g2 = (Graphics2D) g;
g2.setFont(printFont);
g2.setColor(Color.black);
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;
//-----
this.clearSelection();
this.requestFocus();
//-----------
// 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+fontHeight-fontDesent+placeForDel));//bottom right
g2.drawString("Page: "+(pageIndex+1),(int)pageWidth-3*fontWidth,(int)(pageHeight+fontHeight-fontDesent+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);//delimitter
return Printable.PAGE_EXISTS;
}
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;
//-----
this.clearSelection();
this.requestFocus();
//-----------
// 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+fontHeight-fontDesent+placeForDel));//bottom right
g2.drawString("Page: "+(pageIndex+1),(int)pageWidth-3*fontWidth,(int)(pageHeight+fontHeight-fontDesent+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);//delimitter
return Printable.PAGE_EXISTS;
}
