2

I'm just starting to learn about printing with Java Swing, so please bear with me if this question is very naive.

I have a rather complex layout with multiple JPanels that contain other JPanels that contain JLabels. I want somehow to print this nicely on a printer.

I know that I can "paint" on a Graphics2D object that represents a printed page, but that requires me to position each object individually. I would like to be able to use the Swing layout managers to layout the items on my page. One way to do this is to call jp.paint(g2d), where jp is a JPanel and g2d is the Graphics2D object representing the printed page. However, as far as I can see, this will only print a JPanel that is actually visible on the screen. If the JPanel is not visible, it will not be printed.

So is there any way to layout a (rather complex) JPanel and send it to a printer without first displaying the JPanel on the computer screen?

Or am I on a totally wrong track here?

oz1cz
  • 5,504
  • 6
  • 38
  • 58
  • 1
    This will help you--> http://docs.oracle.com/javase/tutorial/2d/printing/gui.html – Java42 Mar 26 '12 at 15:38
  • No, as far as I can see, that talks about printing a visible object. I want to print an invisible object. – oz1cz Mar 28 '12 at 18:40
  • When you build the panel(s) that you want to print, do the pack() but don't setVisible(true) and the panel will print without being displayed on the screen. Give it a try. – Java42 Mar 28 '12 at 19:39
  • I have tried it. I took the example in the URL you mentioned and added this code: `JFrame f2 = new JFrame("xx"); f2.setLayout(new BoxLayout(f2.getContentPane(), BoxLayout.PAGE_AXIS)); f2.add(new JLabel("xxx")); f2.add(new JLabel("yyy")); f2.pack();` I furthermore changed the frame to print from `f` to `f2`. The output is blank! I then added `f2.setVisible(true)` and the frame prints nicely, but is no longer invisible. – oz1cz Mar 29 '12 at 06:24

1 Answers1

2

Stripped down example of how to print a JPanel while invisible.

public class TestPrinterSmall  {
static class JPanelPrintable extends JPanel implements Printable {
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
        if (page > 0) return Printable.NO_SUCH_PAGE;
        printAll(g);  
        return Printable.PAGE_EXISTS;
    }
};
private static void printIt(Printable p) throws PrinterException {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(p);
    if (job.printDialog()) job.print();
}    
public static void main(String args[]) throws PrinterException {
    final JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setSize(400,400);
    final JPanelPrintable j = new JPanelPrintable(); 
    j.setLayout(new BorderLayout());
    j.add(new JButton("1111"),BorderLayout.NORTH);
    j.add(new JButton("2222"),BorderLayout.SOUTH);            
    f.add(j);f.repaint();f.pack();
    //f.setVisible(true);
    printIt(j);
}
}

Output:

(nothing)

Printer:

enter image description here

Java42
  • 7,628
  • 1
  • 32
  • 50
  • That's perfect, Chuck. I still don't understand why my code doesn't work, but now at least I have something I can work with. Thank you very much. – oz1cz Mar 29 '12 at 09:48