打印 JTable 时对齐消息格式
我目前正在使用它来打印我的表格,并且它有效。但我对消息格式的布局不太满意,我希望页脚中包含页码和日期,并且日期格式与表格左侧对齐,页面与右侧对齐。
我怎样才能做到这一点?一直在阅读一些有关重写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以查看此
CustomTablePrintable
。您将表的原始getPrintable()
结果提供给它。在PrinterJob
中,自定义print()
将对表格进行成像,然后在相同的图形上下文中绘制页脚。您可以使用上下文的边界、getFontMetrics()
和stringWidth()
来确定在何处绘制格式化字符串。附录:这是在每页右下角打印灰色日期的示例:
附录:虽然此方法适用于适合单页的表格,但本文介绍了打印 组件大于一页。
You might look at this
CustomTablePrintable
. You feed it your table's unadornedgetPrintable()
result. In yourPrinterJob
, the customprint()
will image the table and then draw your footer in the same graphics context. You can use the context's boundary,getFontMetrics()
andstringWidth()
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:
Addendum: While this approach works for a table that fits on a single page, this article describes printing Components Larger Than One Page.
现在来回尝试一些事情,但仍然无法让任何事情发挥作用。我知道我在这里粘贴的内容可能有点不对劲,但这是尝试各种事情的结果。
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.