在打印JTable时对齐messageformat

时间:2010-02-22 14:36:31

标签: java printing jtable messageformat

我正在使用它来打印我的桌子,它有效。但是我对messageformatting的布局并不满意,我希望在页脚中有pagenumber和date,并且日期格式与表格的左侧对齐,而页面则在右侧。

我该怎么做?正在阅读有关覆盖PrintTable方法的一些内容,但似乎从我读过的内容变得非常复杂。

希望你能帮我解决这个问题,谢谢。 :)

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());
        }
    }

2 个答案:

答案 0 :(得分:3)

您可以查看此CustomTablePrintable。您可以将表格的未加getPrintable()结果输入。在PrinterJob中,自定义print()会对表格进行成像,然后在相同的图形上下文中绘制页脚。您可以使用上下文的边界getFontMetrics()stringWidth()来确定在何处绘制格式化的字符串。

附录:以下是在每页右下角打印灰色日期的示例:

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;
}

附录:虽然此方法适用于适合单个页面的表格,但本文介绍了打印Components Larger Than One Page

答案 1 :(得分:0)

现在来回尝试一些事情仍然无法让任何事情发挥作用。我知道我在这里粘贴的东西可能有点(方式)关闭,但这是尝试各种事情的结果。

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;         
    } 
}