打印时如何在JTable中添加背景图像? [英] How to add background image in JTable when printing?
问题描述
我要打印带有背景图像或水印的JTable
.我的代码是:
I want to print a JTable
with background image or water mark. My code is:
public void actionPerformed(ActionEvent ae)
{
boolean status=false;
MessageFormat header = null;
header = new MessageFormat("Header");
MessageFormat footer = null;
footer = new MessageFormat("Page");
boolean fitWidth = true;
boolean showPrintDialog = true;
boolean interactive = true;
/* determine the print mode */
JTable.PrintMode mode = fitWidth ? JTable.PrintMode.FIT_WIDTH
: JTable.PrintMode.NORMAL;
try
{
status = jt.print(mode, header, footer,showPrintDialog,null,interactive);
if(status ==true)
{
frame.dispose();
}
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
如何通过此方法传递或设置背景图像?
How can I pass or set the background image in this method?
推荐答案
问题有两个部分
-
每页
- 打印一张图片
- 确保图像透视"表
- print an image an each page
- be sure the image "shines-through" the table
第二个问题由@mKorbel解决(尽管由于没有很好的解决方案,所以并未真正解决:-)
the second is addressed by @mKorbel (though not really solved because there is no nice solution :-)
要解决第一个问题,我将选择自定义Printable和子类JTable来返回它,例如
To solve the first, I would go for a custom Printable and subclass JTable to return it, something like
public class BackgroundPrintable implements Printable {
Printable tablePrintable;
JTable table;
MessageFormat header;
MessageFormat footer;
BufferedImage background;
public BackgroundPrintable(MessageFormat header, MessageFormat footer) {
this.header = header;
this.footer = footer;
}
public void setTablePrintable(JTable table, Printable printable) {
tablePrintable = printable;
this.table = table;
}
@Override
public int print(Graphics graphics, PageFormat pageFormat,
int pageIndex) throws PrinterException {
printImage(graphics, pageFormat, pageIndex);
int exists = tablePrintable.print(graphics, pageFormat, pageIndex);
if (exists != PAGE_EXISTS) {
return exists;
}
return PAGE_EXISTS;
}
private void printImage(Graphics graphics, PageFormat pageFormat,
int pageIndex) {
// grab an untainted graphics
Graphics2D g2d = (Graphics2D)graphics.create();
// do the image painting
....
// cleanup
g2d.dispose();
}
}
// use in JTable subclass
@Override
public Printable getPrintable(PrintMode printMode,
MessageFormat headerFormat, MessageFormat footerFormat) {
Printable printable = super.getPrintable(printMode, null, null);
BackgroundPrintable custom = new BackgroundPrintable(headerFormat, footerFormat);
custom.setTablePrintable(this, printable);
return custom;
}
要实现第二个目标,JTable及其呈现器都必须是透明的.棘手的,因为:
To achieve the second, both the JTable and its renderers must be transparent. Tricky, because:
- 可能仅在打印时使用-否则它们应该具有通常的不透明性
- 所有渲染器必须透明,并且没有完全安全的方式来保存所有渲染器
- probably only if printing - otherwise they should have their usual opacity
- all of the renderers must be transparent, and there is no completely safe way to get hold of them all
自定义JTable可以通过在prepareRenderer中强制呈现组件的不透明度来尝试实现这一目标:
A custom JTable could try to achieve that by forcing the rendering component's opacity in its prepareRenderer:
@Override
public Component prepareRenderer(TableCellRenderer renderer,
int row, int column) {
JComponent comp = (JComponent) super.prepareRenderer(renderer, row, column);
if (isPaintingForPrint()) {
comp.setOpaque(false);
} else {
comp.setOpaque(true);
}
return comp;
}
实际上,这并不完全有效:else块中的代码对于自然透明的组件而言可能是错误的做法.恐怕没有真正可靠的解决方案.
Actually, that's not entirely valid: the code in the else block might be the wrong-thing-to-do for naturally transparent components. No really reliable solution available, I'm afraid.
这篇关于打印时如何在JTable中添加背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!