Question

I wrote some code in a JavaFX file. Now I get this ErrorCode:

Exception in thread "usb4java IRP Queue Processor" java.lang.IllegalStateException: Not on FX application thread; currentThread = usb4java IRP Queue Processor
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:210)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:393)
at javafx.scene.Scene.<init>(Scene.java:374)
at javafx.scene.Scene.<init>(Scene.java:232)
at app.model.Window.<init>(Window.java:28)
at app.model.UsbDataInputListener.dataEventOccurred(UsbDataInputListener.java:38)
at org.usb4java.javax.PipeListenerList.dataEventOccurred(PipeListenerList.java:49)
at org.usb4java.javax.Pipe.sendEvent(Pipe.java:270)
at org.usb4java.javax.IrpQueue.finishIrp(IrpQueue.java:48)
at org.usb4java.javax.AbstractIrpQueue.process(AbstractIrpQueue.java:118)
at org.usb4java.javax.AbstractIrpQueue$1.run(AbstractIrpQueue.java:73)
at java.lang.Thread.run(Thread.java:745)

BUILD STOPPED (total time: 41 seconds)

I don't know how to handle this problem.I know that its a problem with a thread(I think), but I don't know what's really wrong, or what this Exception will say to me... Here is some code:

public class UsbDataInputListener implements UsbPipeListener {

@Override
public void errorEventOccurred(UsbPipeErrorEvent upee) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void dataEventOccurred(UsbPipeDataEvent upde) {
    final byte[] recData = upde.getData(); //Übertragene Bytes
    //Input wird überprüft
    //Aktion für die jeweilige Taste wird durchgeführt
    if(Arrays.equals(recData, Keys.POWER)){
        try {
            System.out.println("Power of pressed ...");
            ShutdownInfoController isc = new ShutdownInfoController();
            Window shtdnWin = new Window("/app/view/ShutdownInfoView.fxml","Info zum Herunterfahren",
                    isc,false);

        } catch (IOException ex) {
            Logger.getLogger(UsbDataInputListener.class.getName()).log(Level.SEVERE, null, ex);
        }

    }else if(Arrays.equals(recData, Keys.MUSIC)){

    }else if(Arrays.equals(recData, Keys.GAME)){

    }else if(Arrays.equals(recData, Keys.GALLERY)){

    }else if(Arrays.equals(recData, Keys.VIDEO)){

    }else if(Arrays.equals(recData, Keys.BEFORE)){

    }else if(Arrays.equals(recData, Keys.NEXT)){

    }else if(Arrays.equals(recData, Keys.BACKWARD)){

    }else if(Arrays.equals(recData, Keys.FORWARD)){

    }else if(Arrays.equals(recData, Keys.PLAY)){

    }else if(Arrays.equals(recData, Keys.STOP)){

    }else if(Arrays.equals(recData, Keys.MUTE)){

    }else if(Arrays.equals(recData, Keys.VOL_DOWN)){

    }else if(Arrays.equals(recData, Keys.VOL_UP)){

    }else if(Arrays.equals(recData, Keys.NUMBER_ONE)){

    }else if(Arrays.equals(recData, Keys.UP)){

    }else if(Arrays.equals(recData, Keys.NUMBER_THREE)){

    }else if(Arrays.equals(recData, Keys.LEFT)){

    }else if(Arrays.equals(recData, Keys.ENTER)){

    }else if(Arrays.equals(recData, Keys.RIGHT)){

    }else if(Arrays.equals(recData, Keys.NUMBER_SEVEN)){

    }else if(Arrays.equals(recData, Keys.DOWN)){

    }else if(Arrays.equals(recData, Keys.ESCAPE)){

    }else if(Arrays.equals(recData, Keys.ZERO)){

    }
}

}

public class Window implements Initializable{

private static Stage stage;
// Konstruktor errichtet neues Fenster
public Window(String fxml, String windowName, Object controller, boolean fullscreen ) throws IOException{
    FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml)); //Lädt die zugehörige FXML
    loader.setController(controller); //Setzt den Controller
    Parent root = (Parent) loader.load();
    Scene scene = new Scene(root);
    Window.stage.setTitle(windowName); //Name des Fensters
    Window.stage.setScene(scene);
    Window.stage.setFullScreen(fullscreen); //Setz Vollbild, wenn fullscreen = true
    Window.stage.show(); //Zeigt das Fentsre
}

public static void setStage(Stage stage){
    Window.stage = stage;
}

public static Stage getStage(){
    return Window.stage;
}
//Schließt das Fenster
public static void close(){
    Window.stage.close();
}

@Override
public void initialize(URL location, ResourceBundle resources) {

}

}

public class ShutdownInfoController implements Initializable {

@FXML Button yesButton;
@FXML Button noButton;


public void onYesButtonRequest(ActionEvent e) throws IOException{
    String cmdtext = "shutdown.exe /s /hybrid /t 0 "; // Befehl um PC herunterzufahren
    Runtime.getRuntime().exec(cmdtext); // Befehl wird ausgeführt;
}

public void onNoButtonRequest(ActionEvent e){
    Window.close(); //Fenster wird geschlossen
}

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

Anyone an idea what's wrong??

Was it helpful?

Solution

As the exception indicates, you are operating on the scene graph off of the FX Application Thread. In JavaFX, all operations that manipulate the scene graph must be called on the FX Application Thread. Any operation that changes state of a node or creates a new one must occur on said thread.

Now, your logic is detailed so it is likely that you are operating on the scene graph in multiple locations. For any operation that does such work, run it on the FX Application Thread via Platform.runLater(). The method takes a Runnable that you can use to invoke your scene graph code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top