views:

146

answers:

3

I have been using the printComponent that was shown in another question. It works great for printing a JPanel if I first add that JPanel to a JFrame and draw that JFrame to screen. But if I don't do that before I print, I get a blank page printed. Why is this?

Thanks in advance for any help.

A: 

that is because the panel you wish to draw has an initial size of 0,0. Once added to a container with a layout manager and is displayed, then it gets its "normal" size.

Savvas Dalkitsis
is there any way I can set this to have a size? i tried giving the panel a preferredsize but that didnt work.
try setSize(int width, int height). I'm not sure this will work since Java may still not draw the component but you can try.
Savvas Dalkitsis
+3  A: 

I've used code like the following to create a BufferedImage on a panel that is not visible on the frame:

JPanel panel = new JPanel();
...  // add components
panel.setSize(300, 300);
panel.doLayout();
camickr
A: 

hey i used the setsize and the dolayout method, it works brilliantly. thanks very much for your help.

You should accept camickr's answer so other people know the answer.
Anton
I can't see were to do it. I might not be able to as i am not registered