I've read it already. I obviously didn't quite get it though :)
Can you help with fixing my code?
Main Topics
Browse All TopicsHere 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
scrollPane.setViewportView
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
if (tableWidth >= pageWidth) { scale = pageWidth / tableWidth; }
double headerHeightOnPage = table.getTableHeader().get
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
int numRowsOnFirstPage = numRowsOnAPage - numRowsTakenByTopPanel;
double pageHeightForTableOnFirstP
int totalNumPages = (int)Math.ceil(((double)ta
if(pageIndex >= totalNumPages) { return NO_SUCH_PAGE; }
//move the pen to 0,0 of the page (takes into account margins)
g2.translate(pf.getImageab
//move the pen down below the header
g2.translate(0f,headerHeig
//move the pen all the way back to 0,0
if (pageIndex == 0) {}
else g2.translate(0f,-(pageInde
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
}
else {
g2.setClip(0,
(int)(pageHeightForTable * pageIndex),
(int) Math.ceil(tableWidthOnPage
(int) Math.ceil(pageHeightForTab
}
}
g2.scale(scale,scale);
table.print(g2);
g2.scale(1/scale,1/scale);
g2.translate(0f,pageIndex*
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0,
(int) Math.ceil(tableWidthOnPage
(int)Math.ceil(headerHeigh
g2.scale(scale,scale);
table.getTableHeader().pri
//paint header at top
return Printable.PAGE_EXISTS;
}
private void initComponents() {
scrollPane = new javax.swing.JScrollPane();
getContentPane().setLayout
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I've fixed my code and now it *almost* works. The problems I have now are:
1- sometimes at the bottom or top of a page I get partial printing of a table row. How can I make sure that I don't print partial rows at the bottom of a page? (And conversely that printing at the top of the page starts with a full row)
2- I need to print two copies of the same thing on each page. i.e. this format
---
|A|
---
|A|
---
How can I use the code I have now that prints one copy per page and adapt it so that I print two copies per page.
Thanks!
The new code:
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
if (tableWidth >= pageWidth) { scale = pageWidth / tableWidth; }
double headerHeightOnPage = table.getTableHeader().get
double tableWidthOnPage = tableWidth * scale;
double oneRowHeight = (table.getRowHeight() + table.getRowMargin()) * scale;
int numRowsOnAPage = (int)Math.floor((pageHeigh
double pageHeightForTable = oneRowHeight * numRowsOnAPage;
int numRowsTakenByTopPanel = (int)Math.ceil(topPanelHei
int numRowsOnFirstPage = numRowsOnAPage - numRowsTakenByTopPanel;
double pageHeightForTableOnFirstP
int totalNumPages = (int)Math.ceil(((double)ta
if(pageIndex >= totalNumPages) { return NO_SUCH_PAGE; }
//move the pen to 0,0 of the page (takes into account margins)
g2.translate(pf.getImageab
if (pageIndex == 0) {
topPanel.print(g2);
//put pen below the top panel
g2.translate(0f, topPanelHeight);
g2.scale(scale,scale);
table.getTableHeader().pri
g2.scale(1/scale, 1/scale);
g2.translate(0f, headerHeightOnPage);
g2.setClip(0, 0, (int)Math.ceil(tableWidthO
g2.scale(scale,scale);
table.print(g2);
g2.scale(1/scale, 1/scale);
}
else {
g2.scale(scale,scale);
table.getTableHeader().pri
g2.scale(1/scale, 1/scale);
//needed to place pen below table header
g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -(pageHeightForTable * (pageIndex-1) + pageHeightForTableOnFirstP
g2.setClip(0, (int)Math.ceil(pageHeightF
g2.scale(scale,scale);
table.print(g2);
g2.scale(1/scale, 1/scale);
}
return Printable.PAGE_EXISTS;
}
Business Accounts
Answer for Membership
by: OviPosted on 2002-11-18 at 00:59:59ID: 7462893
http://www.manning.com/sbe /files/swi ng2e/Chapt er22.zip