0

I have a JavaFX program written with Netbeans and Scene Builder. I am able to parse the command line arguments is either the main or start procedure. Based on the argument, I want to execute a procedure and display the results on the form.

This is probably pretty basic, but once I have the startup data, how do I start a procedure in the controller?

How do I pass inpArg to the controller code below. I am getting test data in inpArg.

public void start(Stage stage) throws Exception {
    String inpArg;
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Parameters parameters = getParameters();
    Map<String, String> namedParameters = parameters.getNamed();

    inpArg = "";
    System.out.println("\nnamedParameters -");
    for (Map.Entry<String,String> entry : namedParameters.entrySet()) {
        System.out.println(entry.getKey() + " : " + entry.getValue());
        inpArg += entry.getValue();
    }

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

// Controller code ---------------------

public void SetCmdLine(String cmdLine) {
    lb_cmd_line.setText(cmdLine);
}
GaryK4
  • 419
  • 1
  • 4
  • 9
  • Does this answer your question? [Accessing FXML controller class](https://stackoverflow.com/questions/10751271/accessing-fxml-controller-class) – Slaw Dec 01 '19 at 22:46
  • Does this answer your question? [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – fabian Dec 02 '19 at 06:59
  • I tried multiple tests using the above links. None of them worked for me. I may be doing something wrong, but I could not get a variable set in the FXML file. Being my program uses MySql, I am now setting a DB entry when reading command line, then reading it in the FXML module. I am suprised that JavaFX does not have an easy way of doing this. – GaryK4 Dec 02 '19 at 23:27
  • You mention you can parse the command line arguments in the `Appication#start(Stage)` method, which means you should be familiar with `Application#getParameters()`. You then need to load the FXML file using an `FXMLLoader`. Keep a reference to the loader. Load the root and add it to the scene. Get the controller from the loader. Based on the command line arguments, call whatever method of the controller is appropriate. If that isn't working for you, please create a [mre] demonstrating the problem and then add it to your question via an [edit]. – Slaw Dec 05 '19 at 11:23
  • I understand half of your answer. Isn't root a reference to the loader? I want to pass the string inpArg to the controller. – GaryK4 Dec 08 '19 at 20:04
  • No. The `root` is the result of loading the FXML file which is typically some UI object (e.g. `Parent`). You're using the static `load` method which prevents you from keeping a reference to the loader. Take a closer look at the [second answer](https://stackoverflow.com/a/42072975/6395627) to the question I linked to; notice how it creates, configures, and executes the `FXMLLoader` and then how afterwards it calls `#getController()`. – Slaw Dec 10 '19 at 04:48
  • Also see [How do comment @replies work?](https://meta.stackexchange.com/questions/43019/how-do-comment-replies-work). – Slaw Dec 10 '19 at 04:49

0 Answers0