Ayuda como setear el numero de hojas a imprimir de un JTable

Giancarlo
09 de Febrero del 2006
Saludos, a traves del foro consegui imprimir un JTable, pero el incoveniente es que imprime mas hojas de las que deberia imprimir a pesar de que le pongo en el Dialog (Pag a Imprimr 1 to 1)..........porfa si alguien sabe...que me ayude...aki le envio el codigo .............bueno yo lo puse en una clase..que recibe el JTable a imprimir.....help me!!!

import javax.swing.*;
import java.awt.*;
import java.awt.print.*;


public class cImpresionTabla implements Printable {
JTable tablaAImprimir;

public cImpresionTabla() {
}

public cImpresionTabla(JTable tablaAImprimir){
this.tablaAImprimir = tablaAImprimir;
}


public int print(Graphics g, PageFormat pageFormat,
int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
int fontHeight=g2.getFontMetrics().getHeight();
int fontDesent=g2.getFontMetrics().getDescent();


double pageHeight =
pageFormat.getImageableHeight()-fontHeight;
double pageWidth =
pageFormat.getImageableWidth();
double tableWidth = (double)
tablaAImprimir.getColumnModel(
).getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}

double headerHeightOnPage=
tablaAImprimir.getTableHeader(
).getHeight()*scale;
double tableWidthOnPage=tableWidth*scale;

double oneRowHeight=(tablaAImprimir.getRowHeight()+
tablaAImprimir.getRowMargin())*scale;
int numRowsOnAPage=
(int)((pageHeight-headerHeightOnPage)/
oneRowHeight);
double pageHeightForTable=oneRowHeight*
numRowsOnAPage;
int totalNumPages=
(int)Math.ceil((
(double)tablaAImprimir.getRowCount())/
numRowsOnAPage);
if(pageIndex>=totalNumPages) {
return Printable.NO_SUCH_PAGE;
}

g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());

g2.drawString("Página: "+(pageIndex+1),
(int)pageWidth/2-35, (int)(pageHeight
+fontHeight-fontDesent));

g2.translate(0f,headerHeightOnPage);
g2.translate(0f,-pageIndex*pageHeightForTable);


if (pageIndex + 1 == totalNumPages) {
int lastRowPrinted =
numRowsOnAPage * pageIndex;
int numRowsLeft =
tablaAImprimir.getRowCount()
- lastRowPrinted;
g2.setClip(0,
(int)(pageHeightForTable * pageIndex),
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(oneRowHeight *
numRowsLeft));
}

else{
g2.setClip(0,
(int)(pageHeightForTable*pageIndex),
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(pageHeightForTable));
}

g2.scale(scale,scale);
tablaAImprimir.paint(g2);
g2.scale(1/scale,1/scale);
g2.translate(0f,pageIndex*pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0,
(int) Math.ceil(tableWidthOnPage),
(int)Math.ceil(headerHeightOnPage));
g2.scale(scale,scale);
tablaAImprimir.getTableHeader().paint(g2);



return Printable.PAGE_EXISTS;
}

}