views:

288

answers:

2

I'm using this for the moment to print out my table, and it works. But I'm not really happy with the layout of the messageformatting, I would like to have both pagenumber and date in the footer, and date format aligned to the left side of the table, and page to the right.

How can I do that? Been reading some stuff about overriding the PrintTable method, but seems to get pretty complex from what I've read.

Hope you can help me with this issue, thank you. :)

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.OrientationRequested;
import javax.swing.JTable;
import dk.beesys.rims.ui.WindowInventory;


public class Print {

   private static Print INSTANCE;

   public static Print getInstance() {
      if (INSTANCE == null) {
         INSTANCE = new Print();
      }
      return INSTANCE;
   }

private Print(){ }

    public void printList(java.awt.event.ActionEvent ignore) {
       String strDate = MessageFormat.format("{0,date,short} {0,time,short}", new Date());

        MessageFormat header = new MessageFormat("- {0} -"); 
        MessageFormat footer = new MessageFormat("Printed: " + strDate);
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(OrientationRequested.LANDSCAPE);

        try {

            WindowInventory.getInstance().getTable().print(JTable.PrintMode.FIT_WIDTH, header, footer, true, aset, true);

        } catch (java.awt.print.PrinterException e) {
            System.err.format("Cannot print %s%n", e.getMessage());
        }
    }
A: 

You might look at this CustomTablePrintable. You feed it your table's unadorned getPrintable() result. In your PrinterJob, the custom print() will image the table and then draw your footer in the same graphics context. You can use the context's boundary, getFontMetrics() and stringWidth() to determine where to draw your formatted strings.

Addendum: Here's an example of printing a gray date in the bottom right corner of each page:

public int print(Graphics g, PageFormat pf, int index) throws PrinterException {
    if (index > 0) return NO_SUCH_PAGE;
    String s = new Date().toString();
    Graphics2D g2d = (Graphics2D) g;
    table.getPrintable(
        JTable.PrintMode.NORMAL, null, null).print(g2d, pf, index);
    Rectangle r = g2d.getClipBounds();
    int dw = g2d.getFontMetrics().stringWidth(s);
    int dh = g2d.getFontMetrics().getHeight();
    System.out.println(index + " " + r);
    g2d.setPaint(Color.gray);
    g2d.drawString(s, r.x + r.width - dw, r.y + r.height - dh);
    return Printable.PAGE_EXISTS;
}

Addendum: While this approach works for a table that fits on a single page, this article describes printing Components Larger Than One Page.

trashgod
Been trying to get it to work but without any luck. Any chance that anyone has a code example that would be easier to implement than the CustomTablePrintable example above?
DanielFH
A: 

Been trying some things back and forth now and still not able to get anything to work. I know what I've pasted in here might be a bit (way) off, but it's the result of trying various things.

public class CustomTablePrint implements Printable  {

private static CustomTablePrint INSTANCE = null;

public static CustomTablePrint getInstance() {
    if (INSTANCE == null) {
        INSTANCE = new CustomTablePrint();
    }
    return INSTANCE;
}

private CustomTablePrint() {}


    @Override


    public int print(Graphics graphics, PageFormat pageFormat,  
            int pageIndex) throws PrinterException { 
        if (pageIndex > 0) { 
            return NO_SUCH_PAGE; 
        } 
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(WindowInventory.getInstance().getTable().getPrintable(JTable.PrintMode.FIT_WIDTH, null, null));



        Graphics2D g2d = (Graphics2D)graphics; 
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 

        // Draw header/footer here

        String strDate = MessageFormat.format("{0,date,short} {0,time,short}", new Date());
        String printed = "Printed: ";

        graphics.drawString(printed + strDate, 10, 10); 

        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(OrientationRequested.LANDSCAPE);
        job.printDialog(aset);

        return PAGE_EXISTS;         
    } 
}
DanielFH