This will be a long answer (code wise), because the only solution I found was to implement custom Printable
. Of course, I myself did not write the following code, I basically copied the code extracted from jdk sources and made some settings.
Here we are:
So you said that you are calling the print method:
DefaultTableModel dtm = new DefaultTableModel(new String[] { "Column 1" }, 1); JTable table = new JTable(dtm) { @Override public Printable getPrintable(PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat) { return new TablePrintable(this, printMode, headerFormat, footerFormat); } };
where TablePrintable
is the following class (sorry for not being brief here):
static class TablePrintable implements Printable { private final JTable table; private final JTableHeader header; private final TableColumnModel colModel; private final int totalColWidth; private final JTable.PrintMode printMode; private final MessageFormat headerFormat; private final MessageFormat footerFormat; private int last = -1; private int row = 0; private int col = 0; private final Rectangle clip = new Rectangle(0, 0, 0, 0); private final Rectangle hclip = new Rectangle(0, 0, 0, 0); private final Rectangle tempRect = new Rectangle(0, 0, 0, 0); private static final int H_F_SPACE = 8; private static final float HEADER_FONT_SIZE = 18.0f; private static final float FOOTER_FONT_SIZE = 12.0f; private final Font headerFont; private final Font footerFont; public TablePrintable(JTable table, JTable.PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat) { this.table = table; header = table.getTableHeader(); colModel = table.getColumnModel(); totalColWidth = colModel.getTotalColumnWidth(); if (header != null) {
And here is the result (hope you expect):
source share