MessageFormat头/ footerFormat如何更改JTable打印的字体 [英] MessageFormat header/footerFormat how to change Font for JTable printing
问题描述
到目前为止所显示的片段是,他们不会很好地使用多页面 - 当然,所有作者都知道并提到,因为默认打印认为没有页眉/页脚,并且可以自由使用它们所需的空间。毫不奇怪: - )
所以问题是:有没有办法让默认不打印到页眉/页脚的区域?是的,它是:double-wopper(ehh .. wrapper)是答案 - 使默认打印相信它具有较少的可打印空间通过包装给定的pageFormat返回一个调整getImageableHeight / Y。例如:
public class CustomPageFormat extends PageFormat {
private PageFormat delegate;
私人双头headerHeight;
私人双人footerHeight;
public CustomPageFormat(PageFormat格式,双头headerHeight,双footerHeight){
this.delegate = format;
this.headerHeight = headerHeight;
this.footerHeight = footerHeight;
}
/ **
* @inherited< p>
* /
@Override
public double getImageableY(){
return delegate.getImageableY()+ headerHeight;
}
/ **
* @inherited< p>
* /
@Override
public double getImageableHeight(){
return delegate.getImageableHeight() - headerHeight - footerHeight;
}
//所有其他方法简单地委托
然后在可打印的包装中使用(页脚必须以相似的方式):
public class CustomTablePrintable implements Printable {
可打印表格打印;
JTable表;
MessageFormat头;
MessageFormat页脚;
public CustomTablePrintable(MessageFormat header,MessageFormat footer){
this.header = header;
this.footer = footer;
public void setTablePrintable(JTable table,Printable printable){
tablePrintable = printable;
this.table = table;
$ b @Override
public int print(Graphics graphics,PageFormat pageFormat,
int pageIndex)throws PrinterException {
//获取一个未知图形
Graphics2D g2d =(Graphics2D)graphics.create();
//计算偏移量并包装页面格式
double headerOffset = calculateHeaderHeight(g2d,pageIndex);
CustomPageFormat wrappingPageFormat = new CustomPageFormat(pageFormat,headerOffset,0);
//将包装的pageFormat提供给默认的可打印的
int exists = tablePrintable.print(graphics,wrappingPageFormat,pageIndex);
if(exists!= PAGE_EXISTS){
g2d.dispose();
返回存在;
//将图形转换为原始页面格式的开始并绘制页眉
g2d.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
printHeader(g2d,pageIndex,(int)pageFormat.getImageableWidth());
g2d.dispose();
返回PAGE_EXISTS;
$ b protected double calculateHeaderHeight(Graphics2D g,int pageIndex){
if(header == null)return 0;
Object [] pageNumber = new Object [] {new Integer(pageIndex + 1)};
String text = header.format(pageNumber);
字体headerFont = table.getFont()。deriveFont(Font.BOLD,18f);
g.setFont(headerFont);
Rectangle2D rect = g.getFontMetrics()。getStringBounds(text,g);
return rect.getHeight();
$ b $ protected void printHeader(Graphics2D g,int pageIndex,int imgWidth){
Object [] pageNumber = new Object [] {new Integer(pageIndex + 1)};
String text = header.format(pageNumber);
字体headerFont = table.getFont()。deriveFont(Font.BOLD,18f);
g.setFont(headerFont);
Rectangle2D rect = g.getFontMetrics()。getStringBounds(text,g);
//下面是来自TablePrintable的c& p printText
int tx;
//如果文本足够小以便适合,将其居中
if(rect.getWidth() tx =(int)((imgWidth - rect.getWidth())/ 2);
//否则,如果表是LTR,则确保
的左侧//文本显示;如果(table.getComponentOrientation()。isLeftToRight()){
tx = 0;
//否则,确保文本右边显示
} else {
tx = - (int)(Math.ceil(rect.getWidth()) - imgWidth );
$ b $ int ty =(int)Math.ceil(Math.abs(rect.getY()));
g.setColor(Color.BLACK);
g.drawString(text,tx,ty);
最后从表的getPrintable,如:
final JTable table = new JTable(myModel){
/ **
* @inherited< p>
* /
@Override
public Printable getPrintable(PrintMode printMode,
MessageFormat headerFormat,MessageFormat footerFormat){
可打印printable = super.getPrintable(printMode,null,null );
CustomTablePrintable custom = new CustomTablePrintable(headerFormat,footerFormat);
custom.setTablePrintable(this,printable);
返回自定义;
}
};
printHeader / Footer可以执行任何需要的操作。
在一天结束时:我是否需要调用g.drawString(...)的答案仍然是是。但至少它安全的在桌子外面: - )
in relation to this thread I have a question if someone to know if is possible to override/change larger font (Font Type, Size, Color) for MessageFormat headerFormat comings with JTable.PrintMode or I must paint g2.drawString("my header/footer") and JTable#print() separatelly
解决方案 As everybody already mentioned (while I was relaxing in vacation :-) - TablePrintable is tightly knitted for secrecy, no way to subclass, no way to configure the header/footer printing. The only option to hook is to wrap the table's default printable, let it do its work without header/footer and take over the header/footer printing oneself.
The problem with the snippets shown so far is that they dont play nicely with multi-page - as known and mentioned by all authors, of course - because the default printable thinks there are no headers/footers and freely uses the space required by them. Not surprisingly :-)
So the question is: is there a way to make to default not print into the region of the header/footer? And yeah, it is: double-wopper (ehh .. wrapper) is the answer - make the default printable believe it has less printable space by wrapping the given pageFormat into one that returns a adjusted getImageableHeight/Y. Something like:
public class CustomPageFormat extends PageFormat {
private PageFormat delegate;
private double headerHeight;
private double footerHeight;
public CustomPageFormat(PageFormat format, double headerHeight, double footerHeight) {
this.delegate = format;
this.headerHeight = headerHeight;
this.footerHeight = footerHeight;
}
/**
* @inherited <p>
*/
@Override
public double getImageableY() {
return delegate.getImageableY() + headerHeight;
}
/**
* @inherited <p>
*/
@Override
public double getImageableHeight() {
return delegate.getImageableHeight() - headerHeight - footerHeight;
}
// all other methods simply delegate
Then use in the printable wrapper (footer has to be done similarly):
public class CustomTablePrintable implements Printable {
Printable tablePrintable;
JTable table;
MessageFormat header;
MessageFormat footer;
public CustomTablePrintable(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 {
// grab an untainted graphics
Graphics2D g2d = (Graphics2D)graphics.create();
// calculate the offsets and wrap the pageFormat
double headerOffset = calculateHeaderHeight(g2d, pageIndex);
CustomPageFormat wrappingPageFormat = new CustomPageFormat(pageFormat, headerOffset, 0);
// feed the wrapped pageFormat into the default printable
int exists = tablePrintable.print(graphics, wrappingPageFormat, pageIndex);
if (exists != PAGE_EXISTS) {
g2d.dispose();
return exists;
}
// translate the graphics to the start of the original pageFormat and draw header
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
printHeader(g2d, pageIndex, (int) pageFormat.getImageableWidth());
g2d.dispose();
return PAGE_EXISTS;
}
protected double calculateHeaderHeight(Graphics2D g, int pageIndex) {
if (header == null) return 0;
Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
String text = header.format(pageNumber);
Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
g.setFont(headerFont);
Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
return rect.getHeight();
}
protected void printHeader(Graphics2D g, int pageIndex, int imgWidth) {
Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
String text = header.format(pageNumber);
Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
g.setFont(headerFont);
Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
// following is c&p from TablePrintable printText
int tx;
// if the text is small enough to fit, center it
if (rect.getWidth() < imgWidth) {
tx = (int) ((imgWidth - rect.getWidth()) / 2);
// otherwise, if the table is LTR, ensure the left side of
// the text shows; the right can be clipped
} else if (table.getComponentOrientation().isLeftToRight()) {
tx = 0;
// otherwise, ensure the right side of the text shows
} else {
tx = -(int) (Math.ceil(rect.getWidth()) - imgWidth);
}
int ty = (int) Math.ceil(Math.abs(rect.getY()));
g.setColor(Color.BLACK);
g.drawString(text, tx, ty);
}
}
And at the end return that from table's getPrintable, like:
final JTable table = new JTable(myModel){
/**
* @inherited <p>
*/
@Override
public Printable getPrintable(PrintMode printMode,
MessageFormat headerFormat, MessageFormat footerFormat) {
Printable printable = super.getPrintable(printMode, null, null);
CustomTablePrintable custom = new CustomTablePrintable(headerFormat, footerFormat);
custom.setTablePrintable(this, printable);
return custom;
}
};
printHeader/Footer can be implemented to do whatever is required.
At the end of the day: the answer to the question "do I need to call g.drawString(...)" still is "Yes". But at least it's safely outside of the table itself :-)
这篇关于MessageFormat头/ footerFormat如何更改JTable打印的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!