Created
May 29, 2012 18:45
-
-
Save mabako/2829971 to your computer and use it in GitHub Desktop.
Simple way to print Java GUI elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// beliebige Komponente | |
final Component component = ...; | |
PrinterJob job = PrinterJob.getPrinterJob(); | |
job.setPrintable(new Printable() | |
{ | |
@Override | |
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException | |
{ | |
if(pageIndex > 0) | |
return NO_SUCH_PAGE; | |
component.printAll(graphics); | |
return PAGE_EXISTS; | |
} | |
}); | |
// true, falls der Benutzer das immernoch drucken möchte, false bei Abbruch | |
if(job.printDialog()) | |
{ | |
try | |
{ | |
job.print(); | |
} | |
catch (PrinterException e) | |
{ | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment