0

I've been having difficulties calling a Java file that I've created from another Java file that would contain the UI elements. Here is the Java code that I'm trying to call:

public class XzibitVideo extends Application{

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        String path = "Data/Video/Clip.flv";
        Media media = new Media(new File(path).toURI().toString());

        MediaPlayer mediaPlayer = new MediaPlayer(media);
        MediaView mediaView = new MediaView(mediaPlayer);

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(mediaView);
        //borderPane.setStyle("-fx-background-color: Black");
        //borderPane.setBottom(addToolBar());

        Scene scene = new Scene(borderPane, 1024, 800);
        scene.setFill(javafx.scene.paint.Color.BLACK);

        stage.setTitle("Media Player");
        stage.setScene(scene);
        stage.show();

        mediaPlayer.setAutoPlay(true);
        mediaPlayer.setOnError(()->System.out.println("media error"+ mediaPlayer.getError().toString()));
    }
}

I've tried a couple of methods to call it, but im afraid none of them worked. for example,

XzibitVideo programVideo = new XzibitVideo();
programVideo.start();

XzibitVideo programVideo = new XzibitVideo();
programVideo.run();

XzibitVideo programVideo = new XzibitVideo();
programVideo.main();

*I've also tried adding arguements/parameters, but with no luck:

XzibitVideo programVideo = new XzibitVideo();
programVideo.start(Stage stage);

If anyone has any idea on how to properly call this function i would be extremely grateful! I've been working on this part for way too long, I'm losing all hope.. :(

erikvimz
  • 5,256
  • 6
  • 44
  • 60
user6408978
  • 85
  • 1
  • 2
  • 6
  • `programVideo.start()` - but your `start(Stage stage)` method takes 1 param - Stage, so you probably call start() method from super class and maybe that method in super class does nothing – ikos23 Jun 01 '16 at 10:31
  • programVideo.start(Stage stage); - this is an error. you should pass an argument here : e.g. `Stage s = new Stage(); programVideo.start(s); ` – ikos23 Jun 01 '16 at 10:32
  • `XzibitVideo.main(new String[0]);` or `Application.launch(XzibitVideo.class);` – fabian Jun 01 '16 at 10:33
  • i tried all of your suggesstions, and they all present me with this error "java.lang.IllegalStateException: This operation is permitted on the event thread only" (I believe this is the only part of the error message that might be of some help) – user6408978 Jun 01 '16 at 12:04

4 Answers4

0

CLASSPATH defines where to look for classes. You can set CLASSPATH as environmental variable (in a way depending on your OS), or you can provide it a command-line parameter when invoking Java.

The class files can be in another directory, or in a JAR - anyway you have to point to it with CLASSPATH.

Artur Opalinski
  • 1,052
  • 7
  • 12
  • I'm not quiet sure I understand the relevance here... The problem isnt the ClassPath because it is in the same package as other java files that I have managed to call successfully, However i called them through the .run() function available in them. .. Unfortunetly, this java file, as u can see, doesnt have a run function, instead it has a "start" function, and i'm not sure how to call that :) – user6408978 Jun 01 '16 at 10:31
  • So perhaps your question should be rather: "how to call methods from class `XzibitVideo` , which extends `Application`". Any javadoc for it? As `class XzibitVideo` has a main(), it suggests it can be run as an external program. – Artur Opalinski Jun 01 '16 at 10:38
0

I think this should work:

    XzibitVideo programVideo = new XzibitVideo();
    programVideo.launch(XzibitVideo.class);
    this.dispose();
  • It's bad practice to call a static method from a non-static context. – James_D Jun 01 '16 at 12:16
  • Thanks a million!! you are the best!! This definitely got my code to work, and thanks to everyone else who helped explain this issue to me!! I've been stuck on this part for weeks, almost gave up! Have a great day everyone!! – user6408978 Jun 01 '16 at 12:18
  • I honestly think if you need to do this, you have your program structured incorrectly. – James_D Jun 01 '16 at 12:19
  • after playing around a bit, this code gets it done too (and i think it's faster and safer aswell) `XzibitVideo.launch(XzibitVideo.class);` – user6408978 Jun 01 '16 at 12:20
0

You can launch the application from another class with

Application.launch(XzibitVideo.class);

Note:

  1. this method will block until the JavaFX platform exits
  2. You can only call launch() once per application (i.e. once per JVM lifecycle)

It's not really clear why you want to do this. The start() method in the Application subclass is (as its name implies) the starting point for the application. main methods are only even included in JavaFX applications for the benefit of runtime environments that can't launch JavaFX Application classes natively. Consequently, Application subclasses are inherently not reusable. If you want XzibitVideo to be resuable, you should refactor it so it is not a subclass of Application, and just create a simple Application subclass whose start() method refers to it.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Also see http://stackoverflow.com/questions/32464698/java-how-do-i-start-a-standalone-application-from-the-current-one-when-both-are – James_D Jun 01 '16 at 12:21
  • Thanks alot James, I might need to do more research. I dont like not knowing things, especially when a significant part of my program is using it. also, i've tried `Application.launch(XzibitVideo.class);` and it worked aswell :) thank you so much for your help, I'll keep researching and experimenting and hopefully i'll have a better understanding regarding this. – user6408978 Jun 01 '16 at 12:31
-1

Try:

Stage stage = new Stage(); // or using another constructor of class Stage
XzibitVideo programVideo = new XzibitVideo();
programVideo.start(stage);
Mr.Khoai
  • 1
  • 1
  • I've added the constructor just like you introduced here.. but i'm getting an error now when calling it... "java.lang.IllegalStateException: This operation is permitted on the event thread only" I believe this is the only part of the error message that might be of some help – user6408978 Jun 01 '16 at 11:59