As everyone has already said (while I was relaxing on vacation :-) - TablePrintable is closely related to secrecy, without a subclass, without the ability to customize header / footer printing. The only thing you can do is wrap the default table for printing, let it do its job without a header / footer, and print the header / footer itself.
The problem with snippets, shown so far, is that they do not play well with a multi-page page - as is well known by all authors, of course, because the standard version for printing assumes the absence of headers and footers and free use of space by them. Not surprising: -)
, : / ? , : double-wopper (ehh.. wrapper) - - , , Format , getImageableHeight/Y. - :
public class CustomPageFormat extends PageFormat {
private PageFormat delegate;
private double headerHeight;
private double footerHeight;
public CustomPageFormat(PageFormat format, double headerHeight, double footerHeight) {
this.delegate = format;
this.headerHeight = headerHeight;
this.footerHeight = footerHeight;
}
@Override
public double getImageableY() {
return delegate.getImageableY() + headerHeight;
}
@Override
public double getImageableHeight() {
return delegate.getImageableHeight() - headerHeight - footerHeight;
}
( ):
public class CustomTablePrintable implements Printable {
Printable tablePrintable;
JTable table;
MessageFormat header;
MessageFormat footer;
public CustomTablePrintable(MessageFormat header, MessageFormat footer) {
this.header = header;
this.footer = footer;
}
public void setTablePrintable(JTable table, Printable printable) {
tablePrintable = printable;
this.table = table;
}
@Override
public int print(Graphics graphics, PageFormat pageFormat,
int pageIndex) throws PrinterException {
Graphics2D g2d = (Graphics2D)graphics.create();
double headerOffset = calculateHeaderHeight(g2d, pageIndex);
CustomPageFormat wrappingPageFormat = new CustomPageFormat(pageFormat, headerOffset, 0);
int exists = tablePrintable.print(graphics, wrappingPageFormat, pageIndex);
if (exists != PAGE_EXISTS) {
g2d.dispose();
return exists;
}
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
printHeader(g2d, pageIndex, (int) pageFormat.getImageableWidth());
g2d.dispose();
return PAGE_EXISTS;
}
protected double calculateHeaderHeight(Graphics2D g, int pageIndex) {
if (header == null) return 0;
Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
String text = header.format(pageNumber);
Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
g.setFont(headerFont);
Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
return rect.getHeight();
}
protected void printHeader(Graphics2D g, int pageIndex, int imgWidth) {
Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
String text = header.format(pageNumber);
Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
g.setFont(headerFont);
Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
int tx;
if (rect.getWidth() < imgWidth) {
tx = (int) ((imgWidth - rect.getWidth()) / 2);
} else if (table.getComponentOrientation().isLeftToRight()) {
tx = 0;
} else {
tx = -(int) (Math.ceil(rect.getWidth()) - imgWidth);
}
int ty = (int) Math.ceil(Math.abs(rect.getY()));
g.setColor(Color.BLACK);
g.drawString(text, tx, ty);
}
}
, getPrintable, :
final JTable table = new JTable(myModel){
@Override
public Printable getPrintable(PrintMode printMode,
MessageFormat headerFormat, MessageFormat footerFormat) {
Printable printable = super.getPrintable(printMode, null, null);
CustomTablePrintable custom = new CustomTablePrintable(headerFormat, footerFormat);
custom.setTablePrintable(this, printable);
return custom;
}
};
printHeader/Footer .
: " g.drawString(...)" - "". , , : -)