Skip to content

Instantly share code, notes, and snippets.

@mabako
Created May 29, 2012 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mabako/2829971 to your computer and use it in GitHub Desktop.
Save mabako/2829971 to your computer and use it in GitHub Desktop.
Simple way to print Java GUI elements
// 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