الأحد، 4 مايو 2008

Print Multi line header and footer in JTable

In this post I'll explain how to print the default JTable with a multi line header and multi line footer.

Here's the link to the class MyTablePrintable.

Here's how to use it:


JTable jTable2 = new JTable();

try {
PrinterJob job = PrinterJob.getPrinterJob();
MessageFormat[] header = new MessageFormat[3];
header[0] = new MessageFormat("");
header[1] = new MessageFormat("line 1");
header[2] = new MessageFormat("line 2");

MessageFormat[] footer = new MessageFormat[2];
footer[0] = new MessageFormat("footer 1");
footer[1] = new MessageFormat("footer 2");
job.setPrintable(new MyTablePrintable(jTable2, PrintMode.FIT_WIDTH, header, footer));
job.print();
} catch (PrinterException ex) {
Logger.getLogger(Export.class.getName()).log(Level.SEVERE, null, ex);
}


note that if u want to setup the printer and the printing dialog manipulate the job object.
note also that the 1st header line doesn't appear in the printing so i put an empty line. (bad solution i know, but it's to continue working ;) )