0

I need some help with some actually quite simple code (I think) for work. I have a JFrame that starts an Application to play a video (twice, differenct video each time) and then shows a couple of jButtons to answer questions on the videos. My problem is that I get the first video to show, but then I can't create a new Stage. Maybe you can help me. This is my code in the panel:

public void ausführen(int i, int antwort){

    switch (antwort) {
        case 0:
            //Question or Video                
               switch (i) {
                case 0:
                    //Film1
                    mp = new MediaPanel(); //this is the application
                    mp.run();
                    setVisible(false);
//this timer waits for the video to finish
                    task = new java.util.TimerTask(){
                    @Override
                    public void run(){
                        setVisible(true);                            
                        ausführen(1,0);                            

                    }
                    };
                    t = new java.util.Timer();
                    t.schedule(task, 7000);                                                          
                    break;
                case 1:       //Film2                 
                    mp.restart(new Stage(),"other video");
                    setVisible(false);                        
                    task = new java.util.TimerTask(){
                    @Override
                    public void run(){
                        setVisible(true);                            
                        ausführen(2,0);                                                           
                        dispose();                            
                    }
                    };
                    t = new java.util.Timer();
                    t.schedule(task, 7000);
//next case opens a jPanel with jButtons with the questions (works)

This is the code in MediaPanel (the application):

public class MediaPanel extends Application implements Runnable
{

    private String film = "video1";

    public void setFilm(String f){
        film = f;
    }   

        @Override
    public void run(){
        launch();
    }    
    public void start( Stage stage ) throws Exception
    {
        Group root = new Group();
        stage.setTitle("MoviePlayer");
        Media media = new Media("video1");
        MediaPlayer player = new MediaPlayer(media);
        MediaView view = new MediaView(player);

        view.setPreserveRatio(true);

        root.getChildren().add(view);
        Scene scene = new Scene(root, media.getWidth(), media.getHeight(), Color.BLACK);

        stage.setScene(scene);
        stage.show();
        stage.setFullScreen(false);

        player.play();

        java.util.TimerTask task = new java.util.TimerTask(){
            @Override
            public void run(){
                System.out.print("Still running"); //I wanted to see if this method is called
                Platform.exit();
                }                                              
            };

        java.util.Timer t = new java.util.Timer();
        t.schedule(task, 5000);

    }

    public void restart( Stage stage, String film ) //I created this method to avoid calling launch() twice
    {
        System.out.print("restart");
        Group root = new Group();
        stage.setTitle("MoviePlayer");
        Media media = new Media(film);
        MediaPlayer player = new MediaPlayer(media);
        MediaView view = new MediaView(player);


        view.setPreserveRatio(true);

        root.getChildren().add(view);
        Scene scene = new Scene(root, media.getWidth(), media.getHeight(), Color.BLACK);

        stage.setScene(scene);
        stage.show();
        stage.setFullScreen(true);

        player.play();

        java.util.TimerTask task = new java.util.TimerTask(){
            @Override
            public void run(){
                Platform.exit();
                }                                              
            };

        java.util.Timer t = new java.util.Timer();
        t.schedule(task, 5000);             
    }


    }

I get the error: "Exception in thread "Timer-2" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-2". Alternativley I created a new MediaPanel in MediaWindow, but this did'nt work either because one may not call launch() more than once.

I know my code isn't very good, but this is only supposed to be a really easy GUI/application.

I hope one of you guys can help me! Thank you in advance!

Alex W.
  • 1
  • 1
  • Possible duplicate of [Launch JavaFX application from another class](http://stackoverflow.com/questions/25873769/launch-javafx-application-from-another-class) – fabian Jan 28 '16 at 11:28
  • Your main issue seems to be a misunderstanding about the javafx application lifecycle. You should never create a instance of your `Application` class yourself. Let `Application.launch` do that. – fabian Jan 28 '16 at 11:31
  • 1
    And if you are embedding JavaFX in a Swing application, you shouldn't have an `Application` class at all. See the [`JFXPanel` documentation](http://docs.oracle.com/javase/8/javafx/api/javafx/embed/swing/JFXPanel.html) for examples of using JavaFX in Swing. – James_D Jan 28 '16 at 12:47
  • Great, thanks for the help! – Alex W. Feb 01 '16 at 07:28

0 Answers0