All parts of my Printable Swing component doesn't print
Posted
by Jonas
on Stack Overflow
See other posts from Stack Overflow
or by Jonas
Published on 2010-05-19T11:26:32Z
Indexed on
2010/05/19
11:30 UTC
Read the original article
Hit count: 250
I'm trying to do a printable component (an invoice document). I use JComponent
instead of JPanel
because I don't want a background. The component has many subcomponents.
The main component implements Printable and has a print
-method that is calling printAll(g)
so that all subcomponents should be printed. But my subcomponents doesn't print.
What am I missing? Does all subcomponents also has to implement Printable?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PPanel extends JComponent implements Printable {
static double w;
static double h;
public PPanel() {
this.setLayout(new BorderLayout());
this.add(new JLabel("Document Body"), BorderLayout.CENTER);
this.add(new Header(), BorderLayout.NORTH);
this.add(new Footer(), BorderLayout.SOUTH);
}
class Header extends JComponent {
public Header() {
this.setLayout(new BorderLayout());
this.add(new TopHeader(), BorderLayout.NORTH);
this.add(new LowHeader(), BorderLayout.SOUTH);
}
}
class TopHeader extends JComponent {
public TopHeader() {
this.setLayout(new BorderLayout());
JLabel companyName = new JLabel("Company name");
JLabel docType = new JLabel("Document type");
this.add(companyName, BorderLayout.WEST);
this.add(docType, BorderLayout.EAST);
}
}
class LowHeader extends JComponent {
public LowHeader() {
this.setLayout(new GridLayout(0,2));
JLabel col1 = new JLabel("Column 1");
JLabel col2 = new JLabel("Column 2");
this.add(col1);
this.add(col2);
}
}
class Footer extends JComponent {
public Footer() {
this.setLayout(new GridLayout(0,2));
JLabel addr = new JLabel("Address");
JLabel sum = new JLabel("Sum");
this.add(addr);
this.add(sum);
}
}
public static void main(String[] args) {
final PPanel p = new PPanel();
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(p);
try {
job.print();
} catch (PrinterException ex) {
// print failed
}
// Preview
new JFrame() {{ getContentPane().add(p); this.setSize((int)w, (int)h); setVisible(true); }};
}
@Override
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
w = pf.getImageableWidth();
h = pf.getHeight();
this.setSize((int)w, (int)h);
this.setPreferredSize(new Dimension((int)w, (int)h));
this.doLayout();
this.printAll(g);
return PAGE_EXISTS;
}
}
© Stack Overflow or respective owner