in relation to this thread I have a question if someone to know if is possible to override/change larger font (Font Type, Size, Color) for MessageFormat headerFormat comings with JTable.PrintMode or I must paint g2.drawString("my header/footer") and JTable#print() separatelly
-
I had to use something like [this](http://stackoverflow.com/questions/2311508/aligning-messageformat-on-printing-a-jtable/2313423#2313423), but I'd welcome a better answer. – trashgod May 26 '11 at 21:19
-
@trashgod I saw this thread and read link, before ... that's my "or I must paint g2.drawString", header/footer comes from protected??? java.text Package, and looks like as not possible to overRide this MessageFormat, ... nobody knows, lots of dirtiest hacks around us – mKorbel May 26 '11 at 21:42
-
just to be on the safe side: we agree that a Format has nothing to do with the visual representation properties when painted, right? – kleopatra Jun 14 '11 at 12:02
1 Answers
As everybody already mentioned (while I was relaxing in vacation :-) - TablePrintable is tightly knitted for secrecy, no way to subclass, no way to configure the header/footer printing. The only option to hook is to wrap the table's default printable, let it do its work without header/footer and take over the header/footer printing oneself.
The problem with the snippets shown so far is that they dont play nicely with multi-page - as known and mentioned by all authors, of course - because the default printable thinks there are no headers/footers and freely uses the space required by them. Not surprisingly :-)
So the question is: is there a way to make to default not print into the region of the header/footer? And yeah, it is: double-wopper (ehh .. wrapper) is the answer - make the default printable believe it has less printable space by wrapping the given pageFormat into one that returns a adjusted getImageableHeight/Y. Something like:
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;
}
/**
* @inherited <p>
*/
@Override
public double getImageableY() {
return delegate.getImageableY() + headerHeight;
}
/**
* @inherited <p>
*/
@Override
public double getImageableHeight() {
return delegate.getImageableHeight() - headerHeight - footerHeight;
}
// all other methods simply delegate
Then use in the printable wrapper (footer has to be done similarly):
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 {
// grab an untainted graphics
Graphics2D g2d = (Graphics2D)graphics.create();
// calculate the offsets and wrap the pageFormat
double headerOffset = calculateHeaderHeight(g2d, pageIndex);
CustomPageFormat wrappingPageFormat = new CustomPageFormat(pageFormat, headerOffset, 0);
// feed the wrapped pageFormat into the default printable
int exists = tablePrintable.print(graphics, wrappingPageFormat, pageIndex);
if (exists != PAGE_EXISTS) {
g2d.dispose();
return exists;
}
// translate the graphics to the start of the original pageFormat and draw header
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);
// following is c&p from TablePrintable printText
int tx;
// if the text is small enough to fit, center it
if (rect.getWidth() < imgWidth) {
tx = (int) ((imgWidth - rect.getWidth()) / 2);
// otherwise, if the table is LTR, ensure the left side of
// the text shows; the right can be clipped
} else if (table.getComponentOrientation().isLeftToRight()) {
tx = 0;
// otherwise, ensure the right side of the text shows
} 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);
}
}
And at the end return that from table's getPrintable, like:
final JTable table = new JTable(myModel){
/**
* @inherited <p>
*/
@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 can be implemented to do whatever is required.
At the end of the day: the answer to the question "do I need to call g.drawString(...)" still is "Yes". But at least it's safely outside of the table itself :-)

- 51,061
- 28
- 99
- 211
-
1in due all my respect to your person -1 for JXtable :-), +1 for excelent ... thanks – mKorbel Jun 14 '11 at 16:03
-
just confused, heavens ..., why this very good code missing any up_votes, and very funny in compare with answers about CardLayout have got .... – mKorbel Apr 19 '12 at 14:44
-
Hi, although your solution works, I can't alter printmode to `JTable.PrintMode.FIT_WIDTH` any ideas why? – xpanta Feb 11 '13 at 13:43
-
@xpanta no idea: I would expect the printMode taken just as it is with the unwrapped printable (sorry, currently have no printer near to test) – kleopatra Feb 11 '13 at 14:36
-
@kleopatra I am sorry. `FIT_WIDTH` works when the table is bigger than the page, but it does not enlarge the table to fit the page width (ie when in landscape mode). Is there a solution for this, shall I start another thread? – xpanta Feb 11 '13 at 14:48
-
@xpanta is it working as expected with the original tablePrintable, that is without the wrapper? Anyway, you might be better off starting a new question, then more devs will look at it :-) – kleopatra Feb 11 '13 at 15:43