Create a JavaFX Milestone Inside a Normal Java Application

I have a launcher and a JavaFX class. Launcher creates a class called JavaFXApplication1. JavaFXApplication contains all the JavaFX code (just an example in this case) and should set up a window with one main step.

The launcher has a static main entry point, but I read that JavaFX does not actually use this entry point. This explains my console output (see End of post).

I don't know if this is possible (Launcher creates a JavaFX window - the entry point is not in the presentation class itself). I do not want to use a preloader (I think that preloaders are just for heavy loads during startup) because the launcher is an entire program as a single object (Presentation, business and persistence - 3-layer program). The entry point must be outside the view class (in this example, in the launch class)

The following example works. But for me it looks like a piece of "black magic"

Here is my code

Launcher:

package javafxapplication1; public class Launcher { public static void main(String[] args) { System.out.println("main()"); // Do some stuff and then create the UI class JavaFXApplication1 client = new JavaFXApplication1(); client.caller(args); } } 

JavaFXApplication1:

 package javafxapplication1; import javafx.application.Application; import javafx.stage.Stage; public class JavaFXApplication1 extends Application { @Override public void start(Stage primaryStage) { System.out.println("start()"); primaryStage.setTitle("I am a JavaFX app"); primaryStage.show(); } public void caller(String[] args) { System.out.println("caller()"); launch(args); } /* We call the main function from the client public static void main(String[] args) { launch(args); }*/ } 

And the output for the program:

 start() 

Is there any way to create such an application? thank you

+4
source share
2 answers

The answer to this problem is to create a Java project, not a JavaFX project. After that, you can add the main JavaFX class and write a method (call launch ()).

Perhaps you need to add the compile-time libraries deploy.jar, javaws.jar, jfxrt.jar and plugin.jar from the / jdk _ * / jre / lib directory

+1
source

I wrote a message in Running an instance of JavaFX Application in the main class method - MacDevign

Maybe this is what you are looking for?

The code is quite long, so itโ€™s better to refer to the message, however the use is simple. Note that the init and stop method does not use a start thread, so use it with caution.

The goal is to run a javafx dummy application according to your main method of your class for quick testing / experimentation.

To use this, simply add the following to the main method using lambda, or, alternatively, you can use the anonymous style of the inner class.

 // using the start method of Application class Utility.launchApp((app, stage) -> { // javafx code }, arArgs); 
+1
source

All Articles