How to pass parameters to JavaFX application?

后端 未结 8 548
温柔的废话
温柔的废话 2020-11-28 13:49

I am running my JavaFX application like this:

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


        
相关标签:
8条回答
  • 2020-11-28 14:04

    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);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 14:05

    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.

    0 讨论(0)
  • 2020-11-28 14:06

    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);
    }
    
    0 讨论(0)
  • 2020-11-28 14:10

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

    0 讨论(0)
  • 2020-11-28 14:12

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

    0 讨论(0)
  • 2020-11-28 14:19

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

    }

    0 讨论(0)
提交回复
热议问题