How to pass parameters to JavaFX application?

≡放荡痞女 提交于 2019-12-17 03:20:04

问题


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?


回答1:


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);
    }
}



回答2:


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.




回答3:


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);
}



回答4:


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)
    }
}



回答5:


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).




回答6:


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;
  }

}




回答7:


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.




回答8:


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 ...))



来源:https://stackoverflow.com/questions/24611789/how-to-pass-parameters-to-javafx-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!