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

查看:110
本文介绍了在打印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());
        }
    }

推荐答案

您可能会看到

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天全站免登陆