Printing on JavaFX 1.1 (Applet/Java Web Start)

The way I mentioned yesterday is only good for desktop JavaFX application. If a application is unauthorized applet or Java Web Start application, it doesn't work.

In these case, JNLP API is suitable. However, Java Plug-In supported JNLP API is Java SE 6u10 minimum.

We can use javax.jnlp.PrintService in JNLP API instead of PrinterJob.

The application distinguishs between desktop application and Applet/Java Web Start whether ServiceManager that is a factory class for PrintService is loaded.

Replace defining PrintableNode in yesterday script with the following script:

class PrintableNode extends Group {
    public function print(): Void {
        var printable = FXPrintable {
            comp: this.impl_getSGNode().getPanel()
        }
        try {
            // return value of lookup method is Object,
            // cast it as PrintService
            var service = ServiceManager.lookup("javax.jnlp.PrintService")
                                    as PrintService;
            service.print(printable);
            return;
        } catch (exception: ClassNotFoundException) {
            // fail to load ServiceManager
        } catch (exception: UnavailableServiceException) {
            // doesn't support PrintService
        }
        var job = PrinterJob.getPrinterJob();
        job.setPrintable(printable);
        job.printDialog();
    }
};

When compiling it, javaws.jar is added to class path. javaws.jar is located at jre/lib directory.

Click [Print] Button running by Applet/Java Web Start, then alarm dialog is displayed. When clicking [OK] button, print dialog is displayed.