我正在尝试在打印 JTable 时创建彩色页眉和页脚。 具体来说,我正在查看 javax.swing.JTable 中的 getPrintable(),但 MessageFormat 没有提供指定页眉或页脚颜色的选项。
我该怎么做?
澄清
我有兴趣在打印时设置页眉/页脚。 例如,记事本将文件名作为标题附加到您打印的内容中。
更新
似乎没有标准方法可以做到这一点,有人可以给我一些解决方法吗? 到目前为止发布的唯一答案与打印(如发送到打印机,不显示到屏幕)页眉/页脚无关。
摘自我的评论:我对打印页眉/页脚感兴趣。 例如,当您从记事本打印文档时,它会将文件名附加为标题(或者可能是页脚,我不记得了)
I am trying to create colored headers and footers when printing a JTable. Specifically, I am looking at getPrintable() in javax.swing.JTable, but MessageFormat does not give me the option to specify the color of the header or footer.
How can I do it?
clarification
I am interested in setting the header/footers while printing. For example, notepad appends the filename as a header to what you print.
update
Seems like there is no standard way of doing this, can someone give me some workarounds? The only answer posted so far has nothing to do with printing(as in send to a printer, not displaying to screen) header/footers.
Copied from my comment: I am interested in the printing header/footer. For example, when you are printing a document from notepad, it appends the filename as a header (or perhaps its the footer, I do not remember exactly)
发布评论
评论(4)
我能想到的一种解决方案是使用您自己的可打印:
当您从 JTable 调用 getPrintable 时,将其注入到自定义可打印的新实例中,然后将其与 PrinterJob 一起使用。
现在,您可以根据需要绘制页眉和页脚,但也会丢失一些内容:
编辑:我查看了 Java 源代码,发现私有类 TablePrintable 可以完成所有工作。 您可以查看源代码以查看页眉和页脚是如何打印的。 然后您可以将此功能移至 Printable 类。
One solution I can think of is to use your own printable:
When you call getPrintable from your JTable, inject it to a new instance to the custom printable and then use this with the PrinterJob.
You can now draw the header and footer as you wish, but you also lose some stuff:
EDIT: I've looked at the Java Sources and there is the private class TablePrintable that does all the job. You can peak at the source code to see how the header and footer are printed. Then you can move this functionality to your Printable class.
这段代码主要来自www.java2s.com,并进行了修改以显示如何更改颜色。 这不是最漂亮的解决方案,但希望对您有所帮助。
最重要的是:
请让我知道你的进展如何:)
This code is mainly from www.java2s.com with changes to show how to change the color. It is not the prettiest solution but hopefully will help you.
The most important bit is:
HTH let me know how you got on with it :)
这有点超出了常规,但是您可以使用像 iText 这样的库来创建PDF 文档,然后将其发送到打印机。 您将能够更好地控制文档的外观和演示文稿,包括管理表格的行如何突破页面边界的能力,并且您将能够非常轻松地设置页眉和页脚。
您可以按照以下说明在 Java 中打印 PDF: http://www.juixe.com/techknow/index.php/2008/01/17/print-a-pdf-document-in-java/
This is a little out of left-field, but you could use a library like iText to create a PDF document, and then send that to the printer. You'll get a much greater level of control over the appearance and presentation of the document, including the ability to manage how the rows of your table break over page boundaries, and you'll be able to set the page header and footer very readily.
You can print a PDF in Java following these instructions: http://www.juixe.com/techknow/index.php/2008/01/17/print-a-pdf-document-in-java/
这很容易:
It's easy: