rberman
October 4th, 1999, 01:55 PM
I compiled this test source code I got from somewhere off the Web in my never ending attempt to discover how to print a jTable - hopefully someday from an applet. The code compiles under 1.2.2 on Win95. It builds a JTable of about 75 rows by about 20 columns or so. When I hit the "Print" button, I get the printer dialog box and the first thing I notice is that it thinks there are 9999 pages to print! If I go ahead and click OK - a tremendous print spool file gets created, nothing ever gets printed and then I run out of disk space. So what seems to be the problem with this code? Like I said, I'd really like to just print out an applet on a Web page, but apparently you can't just click on the browser's "Print" button. So I've got to implement printing in Java code, and yet that doesn't seem easy at all. I've spent 3 days just trying to print out a JTable!
mport javax.swing.*;
import javax.swing.table.*;
import java.awt.print.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.Dimension;
public class SalesReport extends JApplet implements Printable
{
JTable tableView;
//JTableHeader tableHeader;
int [] subTableSplit = null;
boolean pageinfoCalculated = false;
int totalNumPages = 0;
int prevPageIndex = 0;
int subPageIndex = 0;
int subTableSplitSize = 0;
double tableHeightOnFullPage, headerHeight;
double pageWidth, pageHeight;
int fontHeight, fontDesent;
double tableHeight, rowHeight;
public void init()
{
final String[] headers = {"Description", "open price", "latest price", "End Date", "Quantity", "a", "b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"};
final Object[][] data =
{
{"Box of Biros", "1.00", "4.99", new Date(), new Integer(2),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
//BUG: If the data element below is uncommented out,
// it will throw an java.lang.IllegalArgumentException: Zero length string passed to TextLayout constructor.
// when it tries to print the empty String.
//{"", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"Blue Biro", "0.10", "0.14", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"tape", "1.00", "1.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.29", new Date(), new Integer(5),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"}
};
TableModel dataModel = new AbstractTableModel()
{
public int getColumnCount() { return headers.length; }
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {return data[row][col];}
public String getColumnName(int column) {return headers[column];}
public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
public boolean isCellEditable(int row, int col) {return (col==1);}
public void setValueAt(Object aValue, int row, int column)
{
data[row][column] = aValue;
}
};
tableView = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(tableView);
scrollpane.setPreferredSize(new Dimension(500, 80));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(BorderLayout.CENTER,scrollpane);
JButton printButton= new JButton();
printButton.setText("print me!");
getContentPane().add(BorderLayout.SOUTH,printButton);
// for faster printing turn double buffering off
RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
printButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(SalesReport.this);
pj.printDialog();
try
{
pj.print();
}
catch (PrinterException e)
{
System.out.println(e);
}
}
});
//setVisible(true); // replaced by code below to run as an application
JFrame frame = new JFrame("Printing a JTable");
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.getContentPane().add(this);
frame.setSize(500,200);
frame.setVisible(true);
}
public static void main(String[] args)
{
SalesReport test = new SalesReport();
test.init();
test.start();
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
{
Graphics2D g2 = (Graphics2D)g;
if(!pageinfoCalculated)
{
getPageInfo(g, pageFormat);
}
g2.setColor(Color.black);
if(pageIndex >= totalNumPages)
{
return java.awt.print.Printable.NO_SUCH_PAGE;
}
if (prevPageIndex != pageIndex)
{
subPageIndex++;
if( subPageIndex == subTableSplitSize - 1)
{
subPageIndex=0;
}
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
int rowIndex = pageIndex / (subTableSplitSize -1);
printTablePart(g2, pageFormat, rowIndex, subPageIndex);
prevPageIndex = pageIndex;
return java.awt.print.Printable.PAGE_EXISTS;
}
public void getPageInfo(Graphics g, PageFormat pageFormat)
{
subTableSplit = null;
subTableSplitSize = 0;
subPageIndex = 0;
prevPageIndex = 0;
fontHeight = g.getFontMetrics().getHeight();
fontDesent = g.getFontMetrics().getDescent();
JTableHeader tableHeader = tableView.getTableHeader();
double headerWidth = tableHeader.getWidth();
headerHeight = tableHeader.getHeight() + tableView.getRowMargin();
pageHeight = pageFormat.getImageableHeight();
pageWidth = pageFormat.getImageableWidth();
double tableWidth = tableView.getColumnModel().getTotalColumnWidth();
tableHeight = tableView.getHeight();
rowHeight = tableView.getRowHeight() + tableView.getRowMargin();
tableHeightOnFullPage = (int)(pageHeight - headerHeight - fontHeight*2);
tableHeightOnFullPage = tableHeightOnFullPage/rowHeight * rowHeight;
TableColumnModel tableColumnModel = tableHeader.getColumnModel();
int columns = tableColumnModel.getColumnCount();
int columnMargin = tableColumnModel.getColumnMargin();
int [] temp = new int[columns];
int columnIndex = 0;
temp[0] = 0;
int columnWidth;
int length = 0;
subTableSplitSize = 0;
while ( columnIndex < columns )
{
columnWidth = tableColumnModel.getColumn(columnIndex).getWidth();
if ( length + columnWidth + columnMargin > pageWidth )
{
temp[subTableSplitSize+1] = temp[subTableSplitSize] + length;
length = columnWidth;
subTableSplitSize++;
}
else
{
length += columnWidth + columnMargin;
}
columnIndex++;
}
if ( length > 0 ) // if are more columns left, part page
{
temp[subTableSplitSize+1] = temp[subTableSplitSize] + length;
subTableSplitSize++;
}
subTableSplitSize++;
subTableSplit = new int[subTableSplitSize];
for ( int i=0; i < subTableSplitSize; i++ )
{
subTableSplit[i] = temp[i];
}
totalNumPages = (int)(tableHeight/tableHeightOnFullPage);
if ( tableHeight%tableHeightOnFullPage >= rowHeight ) // at least 1 more row left
{
totalNumPages++;
}
totalNumPages *= (subTableSplitSize-1);
pageinfoCalculated = true;
}
public void printTablePart(Graphics2D g2, PageFormat pageFormat, int rowIndex, int columnIndex)
{
String pageNumber = "Page: " + (rowIndex+1);
if ( subTableSplitSize > 1 )
{
pageNumber += "-" + (columnIndex+1);
}
int pageLeft = subTableSplit[columnIndex];
int pageRight = subTableSplit[columnIndex + 1];
int pageWidth = pageRight-pageLeft;
// page number message
g2.drawString(pageNumber, pageWidth/2-35, (int)(pageHeight - fontHeight));
double clipHeight = Math.min(tableHeightOnFullPage, tableHeight - rowIndex*tableHeightOnFullPage);
g2.translate(-subTableSplit[columnIndex], 0);
g2.setClip(pageLeft , 0, pageWidth, (int)headerHeight);
JTableHeader tableHeader = tableView.getTableHeader();
tableHeader.paint(g2); // draw the header on every page
g2.translate(0, headerHeight);
g2.translate(0, -tableHeightOnFullPage*rowIndex);
// cut table image and draw on the page
g2.setClip(pageLeft, (int)tableHeightOnFullPage*rowIndex, pageWidth, (int)clipHeight);
tableView.paint(g2); // BUG: this is where the IllegalArgumentException is thrown
// if any cell contains an empty String.
double pageTop = tableHeightOnFullPage*rowIndex - headerHeight;
double pageBottom = pageTop + clipHeight + headerHeight;
g2.drawRect(pageLeft, (int)pageTop, pageWidth, (int)(clipHeight+ headerHeight));
}
}
mport javax.swing.*;
import javax.swing.table.*;
import java.awt.print.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.Dimension;
public class SalesReport extends JApplet implements Printable
{
JTable tableView;
//JTableHeader tableHeader;
int [] subTableSplit = null;
boolean pageinfoCalculated = false;
int totalNumPages = 0;
int prevPageIndex = 0;
int subPageIndex = 0;
int subTableSplitSize = 0;
double tableHeightOnFullPage, headerHeight;
double pageWidth, pageHeight;
int fontHeight, fontDesent;
double tableHeight, rowHeight;
public void init()
{
final String[] headers = {"Description", "open price", "latest price", "End Date", "Quantity", "a", "b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"};
final Object[][] data =
{
{"Box of Biros", "1.00", "4.99", new Date(), new Integer(2),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
//BUG: If the data element below is uncommented out,
// it will throw an java.lang.IllegalArgumentException: Zero length string passed to TextLayout constructor.
// when it tries to print the empty String.
//{"", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"Blue Biro", "0.10", "0.14", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"tape", "1.00", "1.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"stapler", "4.00", "4.49", new Date(), new Integer(1),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"},
{"legal pad", "1.00", "2.29", new Date(), new Integer(5),"a","b", "c", "d", "e", "f", "g", "h", "i","j","k","l","m","n"}
};
TableModel dataModel = new AbstractTableModel()
{
public int getColumnCount() { return headers.length; }
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {return data[row][col];}
public String getColumnName(int column) {return headers[column];}
public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
public boolean isCellEditable(int row, int col) {return (col==1);}
public void setValueAt(Object aValue, int row, int column)
{
data[row][column] = aValue;
}
};
tableView = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(tableView);
scrollpane.setPreferredSize(new Dimension(500, 80));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(BorderLayout.CENTER,scrollpane);
JButton printButton= new JButton();
printButton.setText("print me!");
getContentPane().add(BorderLayout.SOUTH,printButton);
// for faster printing turn double buffering off
RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
printButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(SalesReport.this);
pj.printDialog();
try
{
pj.print();
}
catch (PrinterException e)
{
System.out.println(e);
}
}
});
//setVisible(true); // replaced by code below to run as an application
JFrame frame = new JFrame("Printing a JTable");
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.getContentPane().add(this);
frame.setSize(500,200);
frame.setVisible(true);
}
public static void main(String[] args)
{
SalesReport test = new SalesReport();
test.init();
test.start();
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
{
Graphics2D g2 = (Graphics2D)g;
if(!pageinfoCalculated)
{
getPageInfo(g, pageFormat);
}
g2.setColor(Color.black);
if(pageIndex >= totalNumPages)
{
return java.awt.print.Printable.NO_SUCH_PAGE;
}
if (prevPageIndex != pageIndex)
{
subPageIndex++;
if( subPageIndex == subTableSplitSize - 1)
{
subPageIndex=0;
}
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
int rowIndex = pageIndex / (subTableSplitSize -1);
printTablePart(g2, pageFormat, rowIndex, subPageIndex);
prevPageIndex = pageIndex;
return java.awt.print.Printable.PAGE_EXISTS;
}
public void getPageInfo(Graphics g, PageFormat pageFormat)
{
subTableSplit = null;
subTableSplitSize = 0;
subPageIndex = 0;
prevPageIndex = 0;
fontHeight = g.getFontMetrics().getHeight();
fontDesent = g.getFontMetrics().getDescent();
JTableHeader tableHeader = tableView.getTableHeader();
double headerWidth = tableHeader.getWidth();
headerHeight = tableHeader.getHeight() + tableView.getRowMargin();
pageHeight = pageFormat.getImageableHeight();
pageWidth = pageFormat.getImageableWidth();
double tableWidth = tableView.getColumnModel().getTotalColumnWidth();
tableHeight = tableView.getHeight();
rowHeight = tableView.getRowHeight() + tableView.getRowMargin();
tableHeightOnFullPage = (int)(pageHeight - headerHeight - fontHeight*2);
tableHeightOnFullPage = tableHeightOnFullPage/rowHeight * rowHeight;
TableColumnModel tableColumnModel = tableHeader.getColumnModel();
int columns = tableColumnModel.getColumnCount();
int columnMargin = tableColumnModel.getColumnMargin();
int [] temp = new int[columns];
int columnIndex = 0;
temp[0] = 0;
int columnWidth;
int length = 0;
subTableSplitSize = 0;
while ( columnIndex < columns )
{
columnWidth = tableColumnModel.getColumn(columnIndex).getWidth();
if ( length + columnWidth + columnMargin > pageWidth )
{
temp[subTableSplitSize+1] = temp[subTableSplitSize] + length;
length = columnWidth;
subTableSplitSize++;
}
else
{
length += columnWidth + columnMargin;
}
columnIndex++;
}
if ( length > 0 ) // if are more columns left, part page
{
temp[subTableSplitSize+1] = temp[subTableSplitSize] + length;
subTableSplitSize++;
}
subTableSplitSize++;
subTableSplit = new int[subTableSplitSize];
for ( int i=0; i < subTableSplitSize; i++ )
{
subTableSplit[i] = temp[i];
}
totalNumPages = (int)(tableHeight/tableHeightOnFullPage);
if ( tableHeight%tableHeightOnFullPage >= rowHeight ) // at least 1 more row left
{
totalNumPages++;
}
totalNumPages *= (subTableSplitSize-1);
pageinfoCalculated = true;
}
public void printTablePart(Graphics2D g2, PageFormat pageFormat, int rowIndex, int columnIndex)
{
String pageNumber = "Page: " + (rowIndex+1);
if ( subTableSplitSize > 1 )
{
pageNumber += "-" + (columnIndex+1);
}
int pageLeft = subTableSplit[columnIndex];
int pageRight = subTableSplit[columnIndex + 1];
int pageWidth = pageRight-pageLeft;
// page number message
g2.drawString(pageNumber, pageWidth/2-35, (int)(pageHeight - fontHeight));
double clipHeight = Math.min(tableHeightOnFullPage, tableHeight - rowIndex*tableHeightOnFullPage);
g2.translate(-subTableSplit[columnIndex], 0);
g2.setClip(pageLeft , 0, pageWidth, (int)headerHeight);
JTableHeader tableHeader = tableView.getTableHeader();
tableHeader.paint(g2); // draw the header on every page
g2.translate(0, headerHeight);
g2.translate(0, -tableHeightOnFullPage*rowIndex);
// cut table image and draw on the page
g2.setClip(pageLeft, (int)tableHeightOnFullPage*rowIndex, pageWidth, (int)clipHeight);
tableView.paint(g2); // BUG: this is where the IllegalArgumentException is thrown
// if any cell contains an empty String.
double pageTop = tableHeightOnFullPage*rowIndex - headerHeight;
double pageBottom = pageTop + clipHeight + headerHeight;
g2.drawRect(pageLeft, (int)pageTop, pageWidth, (int)(clipHeight+ headerHeight));
}
}