Printing a JTable in Java.
1 posts in topic
Flat View  Flat View

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 |______|
this.aJBasePanel = aJBasePanel;

public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
if (printJob.printDialog())
try {
} 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;


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=
double tableWidthOnPage=tableWidth*scale;

double oneRowHeight=(tableView.getRowHeight()+
int numRowsOnAPage=

double pageHeightForTable = oneRowHeight * numRowsOnAPage;

int totalNumPages= (int)Math.ceil((

if(pageIndex>=totalNumPages) {
return NO_SUCH_PAGE;



//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.
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
// print the page after setting the scale


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

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

//method to debug
private void log(String 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