0

So when i try to run the java code i get the error

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0

LoginController.java

public class LoginController {

    public void onSessionLoginAction() {
        //I have some functions here so i just minimize a bit of the code
        loginUser();
    }

    public void loginUser() {
        Init.getInstance().hideWindow("login"); // Error <---
        Init.getInstance().setEnabled(true);    // Error <---
        Init.getInstance().run();               // Success <--
    }
}

Init.java

public abstract class Init extends Application {

    private static Init instance;
    private boolean isEnabled = false;
    private Timer timer;

    public void initialize(Stage stage) {
        instance = this;

        // Login with session if you can :) Success to run this code here
        timer = new Timer(50, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FXMLLoader loader = loginWindow.getFxmlLoader();
            LoginController loginController = loader.<LoginController>getController();
            loginController.onSessionLoginAction(); //This runs the session on LoginController.java
            timer.stop();
            }
        });
        timer.start();
    }

    public void setEnabled(boolean flag) {
        isEnabled = flag;
    }

    public static Init getInstance() {
        return instance;
    }
    public abstract void run();
}

1 Answer 1

1

Either run your code in

Platform.runLater(new Runnable() {
    @Override 
    public void run() {
        loginController.onSessionLoginAction();
    }
});

or use Timeline instead of a Timer.

Sign up to request clarification or add additional context in comments.

2 Comments

Omg thanks, i did try a similar code to yours but it did not work. But this works :)
@8803286 The issue is that all stuff that does something with the GUI is supposed to run on the GUI thread. When you are starting a timer, it will run on a different thread. This method allows you to run stuff on the GUI thread from elsewhere. A Timeline is sort of a JavaFX timer and is run on the GUI thread too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.