18

I am running my JavaFX application like this:

public class MainEntry {
    public static void main(String[] args) {
        Controller controller = new Controller();
        Application.launch(MainStage.class);
    }
}

MainStage class extends Appication. Application.launch starts my JavaFX window in a special FX-thread, but in my main method I don't even have an instance of my MainStage class.

How to pass non-String parameter (controller in my case) to MainStage instance? Is it a flawed design?

ferrerverck
  • 618
  • 3
  • 9
  • 17
  • Why can't you simply instantiate the controller inside your application? Btw. `MainStage` doesn't seem to be the right naming for your main application as `Stage` has a different meaning in JavaFX. – isnot2bad Jul 07 '14 at 13:38

8 Answers8

13

Starting with JavaFX 9 you can trigger the launch of the JavaFX platform "manually" using the public API. The only drawback is that the stop method is not invoked the way it would be in application started via Application.launch:

public class MainEntry {
    public static void main(String[] args) {
        Controller controller = new Controller();

        final MainStage mainStage = new MainStage(controller);
        mainStage.init();

        Platform.startup(() -> {
            // create primary stage
            Stage stage = new Stage();

            mainStage.start(stage);
        });
    }
}

The Runnable passed to Platform.startup is invoked on the JavaFX application thread.

fabian
  • 80,457
  • 12
  • 86
  • 114
8

Here's a nice example I found elsewhere

@Override
public void init () throws Exception
{
  super.init ();

  Parameters parameters = getParameters ();

  Map<String, String> namedParameters = parameters.getNamed ();
  List<String> rawArguments = parameters.getRaw ();
  List<String> unnamedParameters = parameters.getUnnamed ();

  System.out.println ("\nnamedParameters -");
  for (Map.Entry<String, String> entry : namedParameters.entrySet ())
    System.out.println (entry.getKey () + " : " + entry.getValue ());

  System.out.println ("\nrawArguments -");
  for (String raw : rawArguments)
    System.out.println (raw);

  System.out.println ("\nunnamedParameters -");
  for (String unnamed : unnamedParameters)
    System.out.println (unnamed);
}
dmolony
  • 1,125
  • 9
  • 23
4

Usually, there is no need to pass arguments to the main application other than the program arguments passed to your main. The only reason why one wants to do this is to create a reusable Application. But the Application does not need to be reusable, because it is the piece of code that assembles your application. Think of the start method to be the new main!

So instead of writing a reusable Application that gets configured in the main method, the application itself should be the configurator and use reusable components to build up the app in the start method, e.g.:

public class MyApplication extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        // Just on example how it could be done...
        Controller controller = new Controller();
        MyMainComponent mainComponent = new MyMainComponent(controller);
        mainComponent.showIn(stage);
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
isnot2bad
  • 24,105
  • 2
  • 29
  • 50
  • 8
    _"The only reason ... is to create a reusable Application"_ I think there are other reasons like when you want a non-fx application launch `Application` (for example fx gui added to existing application). While doing so the non-fx application needs to pass information to the `Application`. – c0der Oct 26 '18 at 06:27
  • @c0der I disagree. There are two ways to start an fx-application from a non-fx Java application: Either as separate process, or in the same VM. The former does not allow to pass anything but strings/streams anyway, so there is no need to have a richer API there. And if you want to start it in the same VM, you can easily write your own `Application` class as 'configuration'-wrapper around the reusable UI-component as described in my answer. Of course this does not work if too much work has to be done in the `Application` class. But this is poor design anyway. – isnot2bad Nov 25 '18 at 16:26
  • 1
    I am not sure I follow, but I think we agree that there may be a need to pass a reference from a non-fx Java application to an `Application`. Maybe you can provide a better answer to [this](https://stackoverflow.com/questions/53003746/passing-reference-to-javafx-application-application) – c0der Nov 25 '18 at 17:02
2

The String array passed to the main() method are the parameters of the application, not specifically to the JavaFX module if you arbitrarily choose to use JavaFX.

The easiest solution could be to store the argumets for later use (e.g. static attribute next to the main() method, and a static getter method to access it).

icza
  • 389,944
  • 63
  • 907
  • 827
2

Question - I

I don't even have an instance of my MainStage class !

Solution

Your main method doesn't need an instance of MainStage to call the start() of your MainStage. This job is done automatically by the JavaFX launcher.

From Docs

The entry point for JavaFX applications is the Application class. The JavaFX runtime does the following, in order, whenever an application is launched:

Constructs an instance of the specified Application class

  1. Calls the init() method
  2. Calls the start(javafx.stage.Stage) method
  3. Waits for the application to finish, which happens when either of the following occur: the application calls Platform.exit() the last window has been closed and the implicitExit attribute on Platform is true
  4. Calls the stop() method

and

The Java launcher loads and initializes the specified Application class on the JavaFX Application Thread. If there is no main method in the Application class, or if the main method calls Application.launch(), then an instance of the Application is then constructed on the JavaFX Application Thread.

Question - II

How to pass non-String parameter (controller in my case) to MainStage instance?

Solution

Why do you need to pass non-String parameter to MainStage? If you need an controller object, just fetch it from the FXML

Example

public class MainEntry extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        Pane pane = (Pane) loader.load(getClass().getResourceAsStream("sample.fxml"));
        //Get the controller
        Controller controller = (Controller)loader.getController();
        Scene scene = new Scene(pane, 200, 200);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);// or launch(MainEntry.class)
    }
}
TuringTux
  • 559
  • 1
  • 12
  • 26
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • Could you explain me how the `stage` parameter is used? Who initializes it? What do you mean by calling `controller object`? Thanks and Regards. – Soner from The Ottoman Empire Aug 26 '17 at 17:22
  • `Stage` is initialized by the [JavaFX launcher](http://hg.openjdk.java.net/openjfx/8/master/rt/file/f89b7dc932af/modules/graphics/src/main/java/com/sun/javafx/application/LauncherImpl.java#l753). It is used to show the application window. – ItachiUchiha Aug 27 '17 at 18:45
0

You can set the Controller in the MainStage class. But you'll have to do it static, otherwise it will be null.

Hava a look at the code:

public class MainEntry {

  public static void main(String[] args) {
    Controller controller = new Controller();
    MainStage ms = new MainStage();
    ms.setController(controller);
    Application.launch(MainStage.class, (java.lang.String[]) null);
  }

}

public class MainStage extends Application {
  private static Controller controller;

  public void start(Stage primaryStage) throws Exception {
    System.out.println(controller);
    primaryStage.show();
  }

  public void setController(Controller controller){
    this.controller = controller;
  }

}

mjaque
  • 472
  • 5
  • 10
0

Of course there is a need and possibility to pass parameters to JavaFX application.

I did it to run my JavaFX client from different places, where different network configurations are required (direct or via proxy). Not to make instant changes in code, I implemented several network configurations to be chosen from in application run command with parameter like --configurationIndex=1. The default code value is 0.

List<String> parameters;
int parameterIndex;
String parameter;

parameters =
  getParameters().getRaw();

for (parameterIndex = 0;
  parameterIndex < parameters.size();
  parameterIndex++) {

  parameter =
    parameters.get(
      parameterIndex);

  if (parameter.contains("configurationIndex")) {
    configurationIndex =
      Integer.valueOf(
        parameters.get(parameterIndex).
        split("=")[1]);
  }
}

In Netbeans you can set this parameter for debugging need directly on your project: Project - Properties - Run - Parameters - insert --configurationIndex=1 into field.

Zon
  • 18,610
  • 7
  • 91
  • 99
0

case 1 = java standard types - transmit them as java Strings "--name=value" and then convert them to the final destination using the answer of dmolony

          for ( Map.Entry<String, String> entry : namedParameters.entrySet ()){
            System.out.println (entry.getKey() + " : " + entry.getValue ());              
            switch( entry.getKey()){
            case "media_url": media_url_received = entry.getValue(); break;
            }
      }

The parameter is created at Application.launch and decoded at init

String args[] = {"--media_url=" + media_url, "--master_level=" + master_level};
Application.launch( args);

case 2 = If you have to transmit java objects use this workaround (this is for only one javafx Application launch, create a Map of workarounds and send index as strings if you have a complex case)

    public static Transfer_param javafx_tp;

and in your class init set the instance of object to a static inside it's own class

    Transfer_param.javafx_tp = tp1;

now you can statically find your last object for working with only one JavaFx Applications (remember that if you have a lot of JavaFx applications active you should send a String with a static variable identification inside a Map or array so you do not take a fake object address from your static structures (use the example at case 1 of this answer to transmit --javafx_id=3 ...))

Dorin
  • 37
  • 7