Imprimir jTable
Yo uso el siguiente c贸digo para imprimir un componente jTable:
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)
MaterialesTable.getColumnModel(
).getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}
double headerHeightOnPage=
MaterialesTable.getTableHeader(
).getHeight()*scale;
double tableWidthOnPage=tableWidth*scale;
double oneRowHeight=(MaterialesTable.getRowHeight()+
MaterialesTable.getRowMargin())*scale;
int numRowsOnAPage=
(int)((pageHeight-headerHeightOnPage)/
oneRowHeight);
double pageHeightForTable=oneRowHeight*
numRowsOnAPage;
int totalNumPages=
(int)Math.ceil((
(double)MaterialesTable.getRowCount())/
numRowsOnAPage);
if(pageIndex>=totalNumPages) {
return 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 =
MaterialesTable.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);
MaterialesTable.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);
MaterialesTable.getTableHeader().paint(g2);
return Printable.PAGE_EXISTS;
}
Funciona perfectamente, pero el problema que tengo es que me gustar铆a imprimir una cabecera al principio y no soy capaz. Si algui茅n puede ayudarme. Gracias.
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)
MaterialesTable.getColumnModel(
).getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}
double headerHeightOnPage=
MaterialesTable.getTableHeader(
).getHeight()*scale;
double tableWidthOnPage=tableWidth*scale;
double oneRowHeight=(MaterialesTable.getRowHeight()+
MaterialesTable.getRowMargin())*scale;
int numRowsOnAPage=
(int)((pageHeight-headerHeightOnPage)/
oneRowHeight);
double pageHeightForTable=oneRowHeight*
numRowsOnAPage;
int totalNumPages=
(int)Math.ceil((
(double)MaterialesTable.getRowCount())/
numRowsOnAPage);
if(pageIndex>=totalNumPages) {
return 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 =
MaterialesTable.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);
MaterialesTable.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);
MaterialesTable.getTableHeader().paint(g2);
return Printable.PAGE_EXISTS;
}
Funciona perfectamente, pero el problema que tengo es que me gustar铆a imprimir una cabecera al principio y no soy capaz. Si algui茅n puede ayudarme. Gracias.
perdona yo estoy intentando imprimir una tabla y he intentado usar el c贸digo que has puesto pero no me funciona me lo podr铆as esplicar un poco??,
a mi me hab铆an dicho que utilizara el m茅todo tabla.printAll(grafico)
pero no me funciona tampoco, ya me parec铆a a mi muy f谩cil, si puedes hecharme una mano te estar铆a muy agradecido. Gracias
a mi me hab铆an dicho que utilizara el m茅todo tabla.printAll(grafico)
pero no me funciona tampoco, ya me parec铆a a mi muy f谩cil, si puedes hecharme una mano te estar铆a muy agradecido. Gracias
Hola, prueba lo siguiente:
copias el c贸digo anterior, que es una funci贸n para imprimir. Donde pongo yo MaterialesTable, t煤 tienes que poner el nombre de la jTable que quieres imprimir.
Y despues pones un bot贸n imprimir que cuando lo pulsen realize la siguientes acciones:
private void ImprimirButtonMouseClicked(java.awt.event.MouseEvent evt) {
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(ConsultarMateriales.this);
pj.printDialog();
try{
pj.print();
}catch (Exception PrintException) {}
}
Donde yo pongo "ConsultarMateriales.this" tienes que poner el nombre de la clase que contiene todo este c贸digo.this.
Ahh se me olvidaba que tambien tienes que poner que la clase qeu contiene este c贸digo implementa Printable:
implements Printable
Y tambien tienes que importar:
import java.awt.print.*;
As铆 te deber铆a funcionar para una tabla sencilla, si tienes alg煤n problema pues me comentas.
copias el c贸digo anterior, que es una funci贸n para imprimir. Donde pongo yo MaterialesTable, t煤 tienes que poner el nombre de la jTable que quieres imprimir.
Y despues pones un bot贸n imprimir que cuando lo pulsen realize la siguientes acciones:
private void ImprimirButtonMouseClicked(java.awt.event.MouseEvent evt) {
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(ConsultarMateriales.this);
pj.printDialog();
try{
pj.print();
}catch (Exception PrintException) {}
}
Donde yo pongo "ConsultarMateriales.this" tienes que poner el nombre de la clase que contiene todo este c贸digo.this.
Ahh se me olvidaba que tambien tienes que poner que la clase qeu contiene este c贸digo implementa Printable:
implements Printable
Y tambien tienes que importar:
import java.awt.print.*;
As铆 te deber铆a funcionar para una tabla sencilla, si tienes alg煤n problema pues me comentas.
