9

考虑以下示例。

如何访问控制器中应用程序的参数/参数?

谢谢你。


注意:我尝试将 App.java 和 MyController.java 混合在一个 Class 文件中,但没有帮助。


App.java(简化):

public class App extends Application {

    public static void main(String[] args) {
        Application.launch(App.class, args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        // output arguments in console
        System.out.println(getParameters().getNamed().toString());

        Parent root = FXMLLoader.load(getClass().getResource("MyView.fxml"));
        final Scene scene = new javafx.scene.Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

MyController.java(简化):

public class MyController implements Initializable {

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // HOW TO getParameters() HERE ?
    }

    @FXML
    private Button myButton;

    @FXML
    private void my_Action(ActionEvent event) {
        // HOW TO getParameters() HERE ?
    }

}

MyView.fxml(简化):

<AnchorPane fx:id="root" fx:controller="mainpackage.MyController">
    <children>
        <Button fx:id="myButton" onAction="#my_Action" text="Start" />
    </children>
</AnchorPane>
4

2 回答 2

17

1.最直接的方式——将它们保存在应用程序中:

public class App extends Application {

  public static void main(String[] args) { launch(); }

  public static String parameters;

  @Override
  public void start(Stage primaryStage) throws Exception {

    parameters = getParameters().getNamed().toString();

    Parent root = FXMLLoader.load(getClass().getResource("MyView.fxml"));
    final Scene scene = new javafx.scene.Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
  }

}

并在控制器中读取它们:

public class MyController implements Initializable {

  @Override
  public void initialize(URL url, ResourceBundle rb) {
     System.out.println(App.parameters);
  }

2.更复杂(但总体上更好)的方法将在下一个主题中描述:

于 2012-04-13T06:48:05.717 回答
0
    public static <T extends Node, P> T load(String resource, final P parameter)
            throws MoisException {
        try {
            logger.debug("resource: {}; parameter: {}", resource, parameter);
            FXMLLoader loader = new FXMLLoader(getResource(resource));
            // pass parameter into Controller,before invoke the initialize()
            loader.setControllerFactory(new Callback<Class<?>, Object>() {
                @Override
                public Object call(Class<?> param) {
                    Object controller = null;
                    try {
                        controller = ReflectUtil.newInstance(param);
                    } catch (InstantiationException e) {
                        throw new MoisException("can't new instance for: " + param.getName(), e);
                    } catch (IllegalAccessException e) {
                        throw new MoisException("can't new instance for: " + param.getName(), e);
                    }
                    if (controller instanceof ParameterAware) {
                        ((ParameterAware<P>) controller).setParameter(parameter);
                    }
                    return controller;
                }
            });
            T node = (T) loader.load();
            // pass parameter to node
            node.setUserData(parameter);
            return node;
        } catch (IOException e) {
            throw new MoisException("can't load the file: " + resource, e);
        }
    }

和参数感知:

public interface ParameterAware<T> {
    void setParameter(T param);
}
于 2013-05-16T08:22:15.170 回答