dcsimg
How do I print a Jtable?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   geert_jordaens
Posted On:   Friday, September 6, 2002 10:05 AM

I'm trying to print a JTable. When I call the print method with the JTable as a component the table is printed without the header. If I add the JTable to a JScrollPane and print the JScrollPane the table is printed with header.
However only the JTable's visible part (within the scrollpane) is printed.

Can anybody tell me how to print a JTable and header?
The scaling to the printer is no problem.

Re: How do I print a Jtable?

Posted By:   virender_singh  
Posted On:   Wednesday, September 11, 2002 12:38 AM


HI
I am sending u the code , if still have any problem mail me at s_virender@hotmail.com.
here table is the object UR table
Call from ur Class Like this
PrintDayWiseTable.PrintComponent(table) ; // table is the object of ur table.
please remove some unnecessary code if u got error .








import java.awt.*;
import java.awt.print.*;
import java.io.PrintStream;
import javax.swing.RepaintManager;
import javax.swing.JTable;
/**
* Title: OnSite Registration System
* Description: This System is used for Onsite Registration and drawing inferences
* based on various registration parameters.
* Copyright: Copyright (c) 2002
* Company: Omnie Solutions (I) Pvt. Ltd.
* @author Virender Singh
* @version 1.0
*/

public class PrintDayWiseTable implements Printable
{
public static void printComponent(JTable table, )
{
(new PrintDayWiseTable(table)).print();
}
public PrintDayWiseTable(JTable table )
{
scale = 0.0D;tableView = table ;
this.totalVisitor = totalVisitor;scale = 0.69999999999999996D;
PrinterJob printerjob = PrinterJob.getPrinterJob();
//pageFormat = new PageFormat();
//pageFormat = printerjob.pageDialog(pageFormat);
pageFormat = printerjob.defaultPage();
}
public void print()
{

PrinterJob printerjob = PrinterJob.getPrinterJob();
double d = pageFormat.getImageableHeight()+50D ;//- 500D;
double d1 = pageFormat.getWidth();
//System.out.println("d =" + d);
//System.out.println("d1= " + d1);

double d2 = pageFormat.getImageableX()/1.5D;// 2D;
double d3 = (pageFormat.getImageableY()/2D)-5 ;

Paper paper = pageFormat.getPaper();
// paper.setSize(612,792);
paper.setImageableArea(d2, d3, d1, d);

pageFormat.setPaper(paper);
// System.out.println("paper = " + pageFormat.getPaper());
// System.out.println("PH =" + pageFormat.getHeight());
// System.out.println("PW =" + pageFormat.getWidth());
printerjob.setPrintable(this, pageFormat);

if(printerjob.printDialog())
{
try
{
System.out.println("Start Printing....");
printerjob.print();
System.out.println("...Printed");
}
catch(Exception exception)
{
System.out.println("Error printing: " + exception);
}
}
}

/* public int print(Graphics g, PageFormat pageformat, int i)
{
double d = pageformat.getImageableHeight();
double d1 = pageformat.getImageableWidth();
double d2 = pageformat.getImageableX();
double d3 = pageformat.getImageableY();
int j = (int)(((double)
if(i == 1)
{
System.out.println("printing ends here");
return 1;
}
else
{
System.out.println("Printing...");
System.out.println("Page Height...." + pageformat.getHeight());
System.out.println("Page Width....." + pageformat.getWidth());
System.out.println("Page Imageable Height...." + pageformat.getImageableHeight());
System.out.println("Page Imageable Width....." + pageformat.getImageableWidth());
System.out.println("ImageaableX...." + pageformat.getImageableX());
System.out.println("ImageaableY...." + pageformat.getImageableY());

Graphics2D graphics2d = (Graphics2D)g;
int fontHeight=graphics2d.getFontMetrics().getHeight();
int fontDesent=graphics2d.getFontMetrics().getDescent();




graphics2d.translate(pageformat.getImageableX(),pageformat.getImageableY());

graphics2d.drawString("Page: "+(i+1),
(int)d1/2-35, (int)(d
+fontHeight-fontDesent));

graphics2d.translate(0f,-i*d);

graphics2d.scale(scale, scale);
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(graphics2d);
enableDoubleBuffering(componentToBePrinted);
return 0;
}
}*/

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();

//leave room for page number
double pageHeight =
pageFormat.getImageableHeight()-fontHeight;
double pageWidth =
pageFormat.getImageableWidth();
double tableWidth = (double)
tableView.getColumnModel(
).getTotalColumnWidth();
double scale = 1;
System.out.println("tableWidth = " + tableWidth);
System.out.println("pageWidth = " + pageWidth);
if (tableWidth >= pageWidth) {
scale =
(pageWidth / tableWidth)-0.1;
}
g2.drawString("Page: "+(pageIndex+1),
(int)pageWidth/2+207, (int)(45));

// String head = categoryName.toUpperCase() +" //DateWise Report ";
//g2.scale(scale,scale);
g2.setFont(new Font("verdana" ,Font.BOLD ,16));
//g2.drawString(head,
//(int)(pageWidth/2-200), (int)(55));

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

double oneRowHeight=(tableView.getRowHeight()+
tableView.getRowMargin())*scale;
int numRowsOnAPage=
(int)((pageHeight-headerHeightOnPage)/
oneRowHeight);
double pageHeightForTable=((oneRowHeight* numRowsOnAPage));

int totalNumPages=
(int)Math.ceil((
(double)tableView.getRowCount())/
numRowsOnAPage);
if(pageIndex>=totalNumPages) {
return NO_SUCH_PAGE;
}

g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
//bottom center
System.out.println("PT -- " + (pageHeight + fontHeight-fontDesent));

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

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

//If this piece of the table is smaller
//than the size available,
//clip to the appropriate bounds.
if (pageIndex + 1 == totalNumPages) {
int lastRowPrinted =
numRowsOnAPage * pageIndex;
int numRowsLeft =
tableView.getRowCount()
- lastRowPrinted;
g2.setClip(0,
(int)(pageHeightForTable * pageIndex),
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(oneRowHeight *
numRowsLeft));
}
//else clip to the entire area available.
else{
g2.setClip(0,
(int)(pageHeightForTable*pageIndex),
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(pageHeightForTable));
}

g2.scale(scale,scale);
tableView.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);
tableView.getTableHeader().paint(g2);
//paint header at top

return Printable.PAGE_EXISTS;
}
public int getNoOfPages()
{
int i = (int)(((double)pageFormat.getImageableHeight()) + 1;
return i;
}

public static void disableDoubleBuffering(Component component)
{
RepaintManager repaintmanager = RepaintManager.currentManager(component);
repaintmanager.setDoubleBufferingEnabled(false);
}

public static void enableDoubleBuffering(Component component)
{
RepaintManager repaintmanager = RepaintManager.currentManager(component);
repaintmanager.setDoubleBufferingEnabled(true);
}

private Component componentToBePrinted;
private double scale;
PageFormat pageFormat;
JTable tableView ;
String questionText ;
int totalVisitor;
String categoryName;
}
About | Sitemap | Contact