Javafx 2.0 How-to Application.getParameters() in a Controller.java file

前端 未结 2 1479
我在风中等你
我在风中等你 2020-12-06 02:38

Considering the following sample.

How to access to arguments/parameters of the application in the controller?

Thank you.


相关标签:
2条回答
  • 2020-12-06 03:08
        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);
            }
        }
    

    and the ParameterAware:

    public interface ParameterAware<T> {
        void setParameter(T param);
    }
    
    0 讨论(0)
  • 2020-12-06 03:17

    1. Most straightforward way -- save them in app:

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

    and read them in controller:

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

    2. More complex (but better in general) approaches are described in next topics:

    • Passing Parameters JavaFX FXML
    • Multiple FXML with Controllers, share object
    0 讨论(0)
提交回复
热议问题