Print JTable - last row gets splitted & header-String problem

J

Juha Rossi

Hi all,
I wrote the following code for printing JTable of multiple pages. I print
using LANDSCAPE in Win. I have 2 Problems:

1) When >= 2 pages, the last row of each page is always splitted between
itself and the next page (half/half per page). How can I fix this?

2) Also I would like to add "header"-String to each page, similar to footer
except that it should be printed on top of each page. With this code
it is printed ok, but the second page misses the Table and it's header???
I think my translate()-methods fail in some point ?!?

HELP Please!

Thanks in advance
Juha Rossi

---------------------------------------
MY CODE SNIPPET:

import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.geom.Rectangle2D;

public class PrintDataView extends Component implements Printable
{
private JTable _printItem;
private String Footer = null;
private String Header = null;
private final static int POINTS_PER_INCH = 27;

// Footer at bottom of page, Header at top of page
public PrintDataView(JTable item, String footer, String header)
{
_printItem = item;
Footer = footer;
Header = header;
}

public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
JTable tableView = modifiedTable();
Graphics2D g2 = (Graphics2D) g;

g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());

int fontHeight = g2.getFontMetrics().getHeight();
int fontDesent = g2.getFontMetrics().getDescent();

double pageHeight = pageFormat.getImageableHeight() - (fontHeight);
double pageWidth = pageFormat.getImageableWidth();

double tableWidth =
(double)tableView.getColumnModel().getTotalColumnWidth();

double scale = 1;

if (tableWidth >= pageWidth)
scale = pageWidth / tableWidth;

double headerHeightOnPage =
tableView.getTableHeader().getHeight() * scale;

double tableWidthOnPage = tableWidth * scale;
double oneRowHeight =
(tableView.getRowHeight() + tableView.getRowMargin()) * scale;

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;
}

if(Header != null)
{
Rectangle2D rect = g2.getFontMetrics().getStringBounds(Header, g2);
double widthOfStr = rect.getWidth();
int centrum = ((int)pageFormat.getPaper().getWidth()/2);

double HeaderXCoord = centrum - (widthOfStr/2);

double HeaderYCoord = fontHeight;

g2.drawString(
Header,
(int)HeaderXCoord,
(int)HeaderYCoord);

// THIS FAILS ????
g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -pageIndex * pageHeightForTable);
}

Rectangle2D rect = g2.getFontMetrics().getStringBounds(pageNbrString, g2);
double widthOfStr = rect.getWidth();
int centrum = ((int)pageFormat.getPaper().getWidth()/2);
double FooterXCoord = centrum - (widthOfStr/2);

double FooterYCoord =
(pageHeight + fontHeight - fontDesent);

g2.drawString(
Footer+" - Page: " + (pageIndex + 1),
(int)FooterXCoord,
(int)FooterYCoord);

g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -pageIndex * pageHeightForTable);

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
{

g2.setClip(
0,
(int) (pageHeightForTable * pageIndex),
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(pageHeightForTable));
}

g2.scale(scale, scale);
tableView.paint(g2);


g2.scale(1 / scale, 1 / scale);
g2.translate(0f, pageIndex * pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);

// SHOULD THIS START AT Y=AFTER "HEADER"-STRING ???
g2.setClip(
0,
0,
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(headerHeightOnPage));

g2.scale(scale, scale);

tableView.getTableHeader().paint(g2);

return Printable.PAGE_EXISTS;
}

private JTable modifiedTable()
{
JTable printItem = _printItem;
JTable tempTable =
new JTable( printItem.getRowCount(),
printItem.getColumnCount());

for (int row = 0; row < printItem.getRowCount(); row++)
{
for (int col = 0; col < printItem.getColumnCount(); col++)
{
Object temp = printItem.getValueAt(row,col);
if (temp == null)
tempTable.setValueAt(" ",row,col);
else
tempTable.setValueAt(temp.toString(),row,col);
}
}
tempTable.setSize(printItem.getSize());
tempTable.setTableHeader(printItem.getTableHeader());
tempTable.setColumnModel(printItem.getColumnModel());

return tempTable;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

cannot print JTable column header 0
How to print?????? 0
JTables 1
printing problem 0

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top