Printing a JFrame and its components
Posted
by
Alex Terreaux
on Stack Overflow
See other posts from Stack Overflow
or by Alex Terreaux
Published on 2012-10-06T23:02:27Z
Indexed on
2012/10/07
3:38 UTC
Read the original article
Hit count: 126
I have been working in a big program and one of its functionalities should be to print the contents of the main window. I checked the API and found this example:
http://docs.oracle.com/javase/tutorial/2d/printing/gui.html
it was very helpful, I tried to use that code in my program by placing this inside the actionperformed method of my print button:
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}
If I click the print button, I get a printer dialog and when I tell it to print, it just prints a blank document. I know the above code is not all I need, as I've seen in the API's examples there is a print() method, but apparently they never call it, so it is pretty confusing. I've tried calling and using it many times, but with no success.
Also, I think that when I finally get it to print, my window will need to be printed in the landscape orientation, it even may need some scaling. Any ideas on how to do that?
I would like any useful help to help me implement this code successfully. I know I should be able to do it by myself just by checking the documentation (I've tried for almost 2 days now) but I can't get it to work. I've learned all the programming I know through the internet. Any help will be greatly appreciated.
© Stack Overflow or respective owner