在打印 JTable 时对齐 messageformat [英] Aligning messageformat on printing a JTable

查看:19
本文介绍了在打印 JTable 时对齐 messageformat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我暂时使用它来打印我的表格,并且它有效.但我对 messageformatting 的布局不太满意,我希望页脚中的页码和日期,以及日期格式与表格左侧对齐,页面向右对齐.

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.

我该怎么做?一直在阅读一些关于覆盖 PrintTable 方法的内容,但从我阅读的内容来看似乎变得非常复杂.

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

推荐答案

你可以看看这个 CustomTablePrintable.您将表格的原始 getPrintable() 结果提供给它.在您的 PrinterJob 中,自定义 print() 将对表格进行成像,然后在相同的图形上下文中绘制页脚.您可以使用上下文的边界,getFontMetrics()stringWidth() 来确定在哪里绘制格式化字符串.

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.

这篇关于在打印 JTable 时对齐 messageformat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆