"不在FX应用程序线程上; currentThread = JavaFX应用程序线程"和" InvocationTargetException"

时间:2016-05-22 09:25:04

标签: error-handling javafx-2 stage

我是java的初学者,所以需要帮助。

我有一个适用于Apache POI的javafx应用程序(适用于Excel文件)。 当我单击menubutton时,我在Controller类中有一个Error,它应该在Map集合中加载Excel数据,然后显示新的Stage窗口。

但是我有一个奇怪的错误"不在FX应用程序线程上; currentThread = JavaFX应用程序线程"按下按钮时。 这是输出窗口中的红色按钮列表,其中包含以下主要标题:

  • 线程中的异常" JavaFX应用程序线程" 了java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    • 引起:java.lang.reflect.InvocationTargetException
    • 引起:java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = JavaFX应用程序线程

以下是Controller in Controller

的代码
public void SKULoading(ActionEvent ae) {
            Stage stage = (Stage) menuBar.getScene().getWindow();
            stage.hide();

            XSSFWorkbook wb = ExcelModel.OpenFile(stage); //using Filechooser
            ExcelModel book = new ExcelModel(wb);
            Map<Integer, List<String>> data = book.SheetToArray(0);
            book.WBclose();

            Stage primarystage;
            // Since that moment error occurs!
            primarystage = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Pane root = null;
            try {
                root = loader.load(getClass().getResource("/FProject/SKUmanager.fxml").openStream());
            } catch (IOException ex) {
                Logger.getLogger(MainWindowController.class.getName()).log(Level.SEVERE, null, ex);
            }
            Scene scene = new Scene(root);
            primarystage.setScene(scene);
            primarystage.show();
} 

我看到了这个话题How to avoid Not on FX application thread; currentThread = JavaFX Application Thread error?,但解决方案就像&#34; Platform.runLater(new Runnable(){});&#34;如果我几乎包含在Method中的所有代码中,似乎解决了这个问题。但我不确定这是不是正确的决定,因为我之前没有创建任何新线程。

  1. 当我删除了行stage.hide();时,显示了新的舞台
  2. 或当我离开行stage.hide();并删除行

        XSSFWorkbook wb = ExcelModel.OpenFile(stage);
        ExcelModel book = new ExcelModel(wb);
        Map<Integer, List<String>> data = book.SheetToArray(0);
        book.WBclose();
    

    再次显示舞台。

  3. 当我替换代码时

    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    File file = fileChooser.showOpenDialog(stage);
    
  4. 方法ExcelModel.OpenFile(stage);中的

    File file = new File("C:\\Users\\Maks\\Desktop\\Test.xlsx");
    

    舞台也被展示了。

    所以,我认为,答案是靠近创建/隐藏阶段或使用FileChooser (看起来像使用Filechooser以某种方式影响新舞台隐藏后的新舞台的外观)

    UPD

    Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
        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:352)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
        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$148(WinApplication.java:191)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
        ... 43 more
    Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = JavaFX Application Thread
        at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
        at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
        at javafx.stage.Stage.<init>(Stage.java:241)
        at javafx.stage.Stage.<init>(Stage.java:227)
        at FProject.MainWindowController.SKULoading(MainWindowController.java:124)
        ... 53 more
    

    帮助我找出问题的根源,以避免将来出现此类错误。谢谢!

0 个答案:

没有答案