0

I'm beginner in java so need help.

I have a javafx app that works with Apache POI (for Excel files). When I click menubutton I have an Error in Controller class which should load Excel data in Map collection and then show new Stage window.

But I have a strange error "Not on FX application thread; currentThread = JavaFX Application Thread" when press the button. THere is a list of red masseges in output window with such main titles:

  • Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    • Caused by: java.lang.reflect.InvocationTargetException
    • Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = JavaFX Application Thread

Here is the code for Method 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();
} 

I saw this topic How to avoid Not on FX application thread; currentThread = JavaFX Application Thread error? but solution like "Platform.runLater(new Runnable(){});" seems to solve the problem if I wrap in it almost all code in the Method. but I'm not sure it is right desicion because I didn't create any new Thread before.

  1. When I removed row stage.hide(); new Stage was shown
  2. Or When I left row stage.hide(); and removed rows

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

    Stage was shown again.

  3. When I replace code

    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    File file = fileChooser.showOpenDialog(stage);
    

in method ExcelModel.OpenFile(stage); by this

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

Stage was also shown.

So, I think, answer is lying near creating/hiding Stages or using FileChooser (Looks like using Filechooser somehow influences on the appearence of a new Stage after privious Stage was hidden)

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

Help me to find out what is the root of problem to avoid such errors in future. Thanks!

Community
  • 1
  • 1
Maks M
  • 11
  • 6
  • Tip:1)JavaFX don't let you change the values of her Elements, for example remove and item from Panel if you are not in javaFX Thread... thats the cause of ( Not on FX application thread;....) – GOXR3PLUS May 22 '16 at 18:11
  • I understand it, and I don't change thread intentionally. It seems that after hiding main window and before creating new Stage, launch of Filechooser changes thread. And what is strange from "FX application thread" to thread with similar name "JavaFX Application Thread". Is it bug?? And when I hide Stage after launch of Filechooser I have no error. Seems like a bug. – Maks M May 22 '16 at 18:56
  • please post those long red messages-(error log) so we can see – Elltz Jun 02 '16 at 17:13

0 Answers0