MessageFormat header/footerFormat 如何更改 JTable 打印的字体 [英] MessageFormat header/footerFormat how to change Font for JTable printing

查看:19
本文介绍了MessageFormat header/footerFormat 如何更改 JTable 打印的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个线程 我有一个问题,如果有人知道是否可以为 MessageFormat headerFormat 来自 JTable.PrintMode 否则我必须分别绘制 g2.drawString("my header/footer") 和 JTable#print()

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

推荐答案

正如大家已经提到的(当我在度假时放松 :-) - TablePrintable 是为了保密而紧密结合的,无法子类化,无法配置标题/页脚打印.钩子的唯一选择是包装表格的默认可打印内容,让它在没有页眉/页脚的情况下工作,并自行打印页眉/页脚.

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 :-)

所以问题是:有没有办法让默认不打印到页眉/页脚的区域?是的,它是:double-wopper (ehh ..wrapper) 是答案 - 通过将给定的 pageFormat 包装成一个返回调整后的 getImageableHeight/Y 的页面格式,让默认的可打印文件相信它的可打印空间更少.类似的东西:

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

    }
}

最后从表的 getPrintable 返回它,例如:

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 可以被实现来做任何需要的事情.

printHeader/Footer can be implemented to do whatever is required.

归根结底:我是否需要调用 g.drawString(...)"这个问题的答案仍然是是".但至少它安全地在桌子外面:-)

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 header/footerFormat 如何更改 JTable 打印的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆