Printing a JTable in Java.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Naveen_Kumar
Posted On:   Wednesday, May 15, 2002 11:03 PM

How to print a JTable using Java?.

I searched on the net for the same but got some complex examples.I heard Java is a simple language but the code seems to be complicated.Is their any simple solution for printing a JTable?

Thanks & Regards.,
Naveen Kumar

Re: Printing a JTable in Java.

Posted By:   Gopal_Krishnan  
Posted On:   Thursday, May 16, 2002 01:05 AM

I was looking for some thing similar and found some material ..


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


public class JTablePrintUtility implements Printable {

/**
* A generic component to print any component
*/
private Component aJBasePanel;

/**
* handle to the format of the apge being printed
*/
private PageFormat pageFormat;

/**
* public method which has to be called from an external class
* to print the JTable
*/
public static void printComponent(Component c) {
new JTablePrintUtility(c).print();
}

/**
* constructor
*/
public JTablePrintUtility(Component aJBasePanel) {
pageFormat = new PageFormat();
// 4 PageAttributes.OrientationRequestedType.LANDSCAPE |______|
pageFormat.setOrientation(PageFormat.LANDSCAPE);
this.aJBasePanel = aJBasePanel;
}

public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this,pageFormat);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}


/**
* This method is invoked for every page printed and is the
* implementation for the printable interface
*/
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
// initialing the table to be printed .. this prints only jtables
JTable tableView = (JTable) aJBasePanel;

log("pageIndex" +pageIndex);
// casting the graphics object to g2d object
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;

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

//If table width is greater than pagewidth the table is shrunk by a factor!!
if (tableWidth >= pageWidth) {
log("pageWidth : "+pageWidth );
log("tableWidth : "+tableWidth);
scale = pageWidth / tableWidth;
log("scale(pw/tw): "+scale );
}

log("scale: "+scale );
double headerHeightOnPage=
tableView.getTableHeader().getHeight()*0.75;
double tableWidthOnPage=tableWidth*scale;

double oneRowHeight=(tableView.getRowHeight()+
tableView.getRowMargin())*0.75;
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());

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));
}
// The vertical scale has been fixed at 0.75 to avoid scaling too low
g2.scale(scale,0.75);
// print the page after setting the scale

tableView.paint(g2);
g2.scale(1/scale,1/0.75);

g2.translate(0f,pageIndex*pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage),
(int)Math.ceil(headerHeightOnPage));
g2.scale(scale,0.75);
tableView.getTableHeader().paint(g2);//paint header at top

return Printable.PAGE_EXISTS;
} // end of method print

//method to debug
private void log(String logme){
System.out.println(logme);
}

}


Just instantiate the above class passing the JTable in the constructor
and call the print method
You can adjust the variables.
I have set the default orientation as landscape as our tabels are big .. It will bring up the printer dialog and you can change the settings if required

hope this helps ...
About | Sitemap | Contact