打印 JTable 时对齐消息格式

发布于 2024-08-23 01:15:36 字数 1357 浏览 3 评论 0原文

我目前正在使用它来打印我的表格,并且它有效。但我对消息格式的布局不太满意,我希望页脚中包含页码和日期,并且日期格式与表格左侧对齐,页面与右侧对齐。

我怎样才能做到这一点?一直在阅读一些有关重写 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());
        }
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2024-08-30 01:15:36

您可以查看此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;
}

附录:虽然此方法适用于适合单页的表格,但本文介绍了打印 组件大于一页

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.

风筝有风,海豚有海 2024-08-30 01:15:36

现在来回尝试一些事情,但仍然无法让任何事情发挥作用。我知道我在这里粘贴的内容可能有点不对劲,但这是尝试各种事情的结果。

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

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;         
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文