Javafx-如何为主应用程序设置非静态值

时间:2019-05-27 16:13:53

标签: java javafx deep-linking

我想在主应用程序中设置一个侦听器。 一些值将在运行时发送到应用程序。 工作流将是:

  • getParameters()
  • 获取值
  • 将新值通知侦听器。
  • 在控制器上接收该值。

我只是将代码设置如下:

公共类MainApp扩展了应用程序{

private DeeplinkListener listener;

@Override
public void init() throws Exception {
    super.init(); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void start(Stage stage) throws Exception {
    //check if it is running
    List<String> rawArgs = getParameters().getRaw();
    if (rawArgs == null) {
        rawArgs = new ArrayList<>();
    }

    String[] args = rawArgs.toArray(new String[rawArgs.size()]);

    boolean isRunning = LocalClientSocketHandler.checkProcess(args);
    if (isRunning) {
        System.exit(0);
    }

    startApplication(stage, args);
}

private void startApplication(Stage stage, String[] args) throws IOException {

    StackPane rootNode = new StackPane(); // Create root pane
    rootNode.setStyle("-fx-background-color: transparent;");
    Scene scene = new Scene(rootNode);
    stage.setScene(scene);

    // this object represent the stack  of activities  in your application
    CurrentStageHandler.getInstance().setCurrentStage(stage);
    ActivityFactory factory = new ActivityFactory(stage);
    HashMap<String, String> extras = DeepLinkHandler.getDeepLinkArguments(getParameters());
    if(listener != null) listener.onReceive(extras);

    Intent intent = new Intent(extras, MainController.class);
    factory.startActivity(intent);
}

public void addListener(DeeplinkListener listener) {
    this.listener = listener;
}

/**
 * The main() method is ignored in correctly deployed JavaFX application.
 * main() serves only as fallback in case the application can not be
 * launched through deployment artifacts, e.g., in IDEs with limited FX
 * support. NetBeans ignores main().
 *
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

我的问题是:如何在控制器中设置此侦听器?

0 个答案:

没有答案