Question

i am trying use the design pattern on my Mediator. In order to have my gui seperated instead of having all components in one class.

And example would be that you needed to be logged into the program before using other GUI components. Therefore creating a Mediator class that creates an instance of each gui element that each of the GUI classes (Login, addUser, ShowUser) could refer to when changeing window.

 public class Mediator {

        public Login login;
        public AddUser add;
        public ShowUsers su;
        public Stage stage = new Stage();
        public Mediator(){
            login = new Login(this);
            add = new AddUser(this);
            su = new ShowUsers(this);
        }
        public void showUser() throws Exception{
            su.start(stage);
        }
    }
public class ShowUsers extends Application{
    private Mediator m;
    private Stage stage = new Stage();
    public ShowUsers(Mediator m){
        this.m =m;
    }
    @Override
    public void start(Stage stage) throws Exception {
        Group root = new Group();
        Scene scene = new Scene(root);

        stage.setScene(scene);

    }

}

I am getting an exepction saying:Exception in thread "main" java.lang.IllegalStateException: Not on FX application thread; currentThread = main

How do i get around this if i want to use a mediator?

Update

public class Main {

    public static void main(String[] args) throws Exception{
        Mediator m = new Mediator();
        m.showUser();

    }

}
Was it helpful?

Solution

You start a Java FX Application with Application.launch. I don't think you can launch more than one Application per JVM, but I haven't tried (why would you want?). Of course you can have several Stages per application though.

Edit: In fact the Javadoc says: " It must not be called more than once or an exception will be thrown. "

http://docs.oracle.com/javafx/2/api/javafx/application/Application.html#launch(java.lang.String...)

OTHER TIPS

I believe you are looking for Platform.runLater() (See Platform).

This will place your application on the correct thread. However, you still have to think about initializing the JavaFX Toolkit by either a) extending Application with Main instead of ShowUsers or b) using a JFXPanel if you are using this from within a Swing application.

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