Add text to print before and after JTable

I am trying to print the JTable method, and the print() method works fine until I come to this script. Suppose I want to print before, on the first page (and not the headline), the text "Report", and at the end the text "This is the end of the report." I would like to clarify again that I do not need a header or footer, only this text will appear at the top of the first and bottom of the last page when I print them.

How can i do this?

+6
source share
1 answer

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

Application: JTable has a getPrintable() method that should simplify things; here is the diagram and the simplest name 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; } } 
+4
source

Source: https://habr.com/ru/post/913736/


All Articles