Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

java - Append text for printing before and after a JTable

I am trying to print a JTable and the print() method works great till I come to this scenario. Lets say I want to print before, in the first page only (not header) the text "Report" and on the end the text "This is the end of report". I would like once more to clarify that I don't need a header or footer only this text to appear in the top of the first and bottom of the last page when I print them.

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way to do this is to append() a series of suitable Printable instances to a java.awt.print.Book, as shown here.

Addendum: JTable has a getPrintable() method that should simplify things; here's an outline and simple title Printable:

PrinterJob pj = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new Title(), pj.defaultPage());
book.append(table.getPrintable(...), pj.defaultPage());
book.append(new EndPage(), pj.defaultPage());
pj.setPageable(book);
pj.print();
...
private static class Title implements Printable {

    Font font = new Font("SansSerif", Font.PLAIN, 48);

    @Override
    public int print(Graphics g, PageFormat pf, int pageIndex)
        throws PrinterException {
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        g2d.setFont(font);
        g2d.setColor(Color.black);
        g2d.drawString("Report", 50, 200);
        return Printable.PAGE_EXISTS;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...