code: public void print(Printable painter) {
          printer.setPrintable(painter);
          if (printer.printDialog(printAttributes)) {
              try {
                  printer.print(printAttributes);
              } catch (PrinterException ex) {
                  logger.error("Failed to print", ex);
                  JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
              }
          }
      }
docs: Prints a document that implements Printable interface.
  @param painter the Printable that renders each page of the document.