-1

I've tried everything I can think of, I'm hoping someone out there might have some knowledge I don't.

I'm trying to print an HTML document loaded into a JavaFX webview control, and I keep getting an IllegalStateException violation(Even though it still prints). Originally the code was being run inside an event handler so I thought that might be the issue. I tried moving the actual print code to the FX/UI thread, into it's own thread, and even a listener.(it's currently in the listener configuration) all produce the same exceptions.

Random data points:

  1. There are always exactly 7 exceptions thrown regardless the size of the HTML document.
  2. The exceptions are coming from inside the WebEngine.print() method.

I'm really hoping someone out there has an idea what might be wrong.
Thanks,
TR

Here's the code.

      public class PostView {
        @FXML protected WebView webviewer;
        protected BooleanProperty printReq = new SimpleBooleanProperty(false);
        protected WebEngine engine; 

      @FXML
      public void initialize() {
         engine = webviewer.getEngine();

         printReq.addListener(new ChangeListener<Boolean>() {
              @Override
              public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                  // Only if completed
                  if (newValue) {
                    if (Security.okayToPrint()) {
                      PrinterJob job = PrinterJob.createPrinterJob(Printer.getDefaultPrinter());
                      if (job != null) {
                        if (job.showPrintDialog(Info.windowHandle)) {
                          Squirt.out(5, "PostView - User wants to print a page!!!!");
                          engine.print(job); // <- Exceptions squirt out here.
                          job.endJob();
                        }
                      } else {
                        Log.warning("Could not create a print job");
                        final Shout shout = new Shout(AlertType.ERROR);
                        shout.setContentText(Info.lexicon.shipLogPrintErrorMsg);
                        shout.showAndWait();
                      }
                    }
                  }
                  printReq.set(false);
              }
            });
        }
    }

Here's the Exception trace:

***java.lang.IllegalStateException:** the method should not be called in this context
    at com.sun.javafx.webkit.theme.RenderThemeImpl.ensureNotDefault(RenderThemeImpl.java:250)
    at com.sun.javafx.webkit.theme.RenderThemeImpl.createWidget(RenderThemeImpl.java:263)
    at com.sun.webkit.WebPage.twkPrint(Native Method)
    at com.sun.webkit.WebPage.lambda$print$46(WebPage.java:1851)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._enterNestedEventLoopImpl(Native Method)
    at com.sun.glass.ui.win.WinApplication._enterNestedEventLoop(WinApplication.java:204)
    at com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:511)
    at com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
    at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:590)
    at com.sun.prism.j2d.print.J2DPrinterJob$J2DPageable.implPrintPage(J2DPrinterJob.java:1146)
    at com.sun.prism.j2d.print.J2DPrinterJob$J2DPageable.access$500(J2DPrinterJob.java:972)
    at com.sun.prism.j2d.print.J2DPrinterJob.print(J2DPrinterJob.java:797)
    at javafx.print.PrinterJob.printPage(PrinterJob.java:398)
    at javafx.print.PrinterJob.printPage(PrinterJob.java:414)
    at javafx.scene.web.WebEngine.print(WebEngine.java:1649)
    at com.trmartin.controls.PostView$1.changed(PostView.java:135)
    at com.trmartin.controls.PostView$1.changed(PostView.java:125)
    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
    at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
    at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
    at com.trmartin.controls.PostView$PrintRequest.handle(PostView.java:611)
    at com.trmartin.controls.PostView$PrintRequest.handle(PostView.java:606)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at com.trmartin.controls.TheMenu.menuPrint(TheMenu.java:1049)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)*
  • 2
    Is this code missing something? `protected engine = webviewer.getEngine();` – SedJ601 May 22 '18 at 20:50
  • 1
    Even if you provide a type there, it won't work because `webviewer` won't be initialized when the object initializer is executed. – James_D May 22 '18 at 20:52
  • Sorry guys, the actual class is pretty large so I short handed it. I only included parts that pertained to the issue at hand. The Protected engine = webviewer.getEngeine() and the class listener addition is in the initialize() routine of the class. – TR. Martin May 22 '18 at 21:08
  • 1
    You can’t declare a local variable as `protected` – James_D May 22 '18 at 21:22
  • James_D yea bad transcription from the full class to the snippet: the engine variable is declared in the class and fulfilled in the initialize() method. The problem still exists. There's something funky going on in the WebEngine when it tries to use the PrintJob I provide. – TR. Martin May 23 '18 at 16:51

1 Answers1

0

I have same trouble. My solutions were:

  1. With java8 version 121, it works without exceptions.
  2. With java8 version 172 and code below, it works anyway after a lot of exceptions.

    try { engine.print(job); } finally { job.endJob(); }

This page may give you some ideas: https://bugs.java.com/view_bug.do?bug_id=8097381

sakura
  • 1
  • 1