Here are the specs:
1- Print out the contents of a JTable which may span multiple pages
2- At the top of the first page print out a JPanel
This should be easy but I'm just not understanding how to use translate() and clip().
Here's the code I have so far, but it isn't printing things properly at all ...
package TAL.TALObjects;
import TAL.Panels.*;
import java.awt.print.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class Preview extends javax.swing.JFrame implements Printable {
private JTable table;
private CustomerReceiptTopPanel topPanel;
private int tableWidth = 0;
public Preview(JTable table, CustomerReceiptTopPanel topPanel) {
this.table = table;
this.topPanel = topPanel;
setColumnWidths(table); //makes the tablecolumns as wide as needed
initComponents();
getContentPane().add(topPa
nel, new java.awt.GridBagConstraint
s());
scrollPane.setViewportView
(table);
pack();
setSize(tableWidth + 100, 800);
setVisible(true);
}
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
double scale = 1;
double topPanelYClip = 0;
g2.setColor(Color.black);
//how much room will the Header of the first page take?
double topPanelHeight = topPanel.getHeight();
//calculate the scaling factor (if needed)
double pageHeight = pf.getImageableHeight();
double pageWidth = pf.getImageableWidth();
double tableWidth = (double)table.getColumnMod
el().getTo
talColumnW
idth();
if (tableWidth >= pageWidth) { scale = pageWidth / tableWidth; }
double headerHeightOnPage = table.getTableHeader().get
Height() * scale;
double tableWidthOnPage = tableWidth * scale;
double oneRowHeight = (table.getRowHeight() + table.getRowMargin()) * scale;
int numRowsOnAPage = (int)((pageHeight - headerHeightOnPage) / oneRowHeight);
double pageHeightForTable = oneRowHeight * numRowsOnAPage;
int numRowsTakenByTopPanel = (int)Math.ceil(topPanelHei
ght / oneRowHeight);
int numRowsOnFirstPage = numRowsOnAPage - numRowsTakenByTopPanel;
double pageHeightForTableOnFirstP
age = oneRowHeight * numRowsOnFirstPage;
int totalNumPages = (int)Math.ceil(((double)ta
ble.getRow
Count() - numRowsOnFirstPage) / numRowsOnAPage) + 1;
if(pageIndex >= totalNumPages) { return NO_SUCH_PAGE; }
//move the pen to 0,0 of the page (takes into account margins)
g2.translate(pf.getImageab
leX(), pf.getImageableY());
//move the pen down below the header
g2.translate(0f,headerHeig
htOnPage);
//move the pen all the way back to 0,0
if (pageIndex == 0) {}
else g2.translate(0f,-(pageInde
x - 1) * pageHeightForTable - pageHeightForTableOnFirstP
age);
if (pageIndex == 0) {
//set clip for printing only the top panel
g2.setClip(0,0, (int)topPanelHeight, (int)tableWidthOnPage);
topPanel.print(g2);
}
//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 - numRowsTakenByTopPanel;
int numRowsLeft = table.getRowCount() - lastRowPrinted;
g2.setClip(0,
(int)(pageHeightForTable * pageIndex + topPanelHeight),
(int) Math.ceil(tableWidthOnPage
),
(int) Math.ceil(oneRowHeight * numRowsLeft));
}
//else clip to the entire area available.Unless this is the first page ...
else{
if (pageIndex == 0) {
g2.setClip(0,
(int)(topPanelHeight),
(int) Math.ceil(tableWidthOnPage
),
(int) Math.ceil(pageHeightForTab
leOnFirstP
age));
}
else {
g2.setClip(0,
(int)(pageHeightForTable * pageIndex),
(int) Math.ceil(tableWidthOnPage
),
(int) Math.ceil(pageHeightForTab
le));
}
}
g2.scale(scale,scale);
table.print(g2);
g2.scale(1/scale,1/scale);
g2.translate(0f,pageIndex*
pageHeight
ForTable);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0,
(int) Math.ceil(tableWidthOnPage
),
(int)Math.ceil(headerHeigh
tOnPage));
g2.scale(scale,scale);
table.getTableHeader().pri
nt(g2);
//paint header at top
return Printable.PAGE_EXISTS;
}
private void initComponents() {
scrollPane = new javax.swing.JScrollPane();
getContentPane().setLayout
(new java.awt.GridBagLayout());
}
}