16

I have the following code:

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) throws Exception{
      Parent root = FXMLLoader.load(getClass().getResource("hive.fxml"));
      primaryStage.setTitle("Hive-viewer");
      primaryStage.setScene(new Scene(root, 1600, 900));
      primaryStage.show();
  }


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

I want to know how you would use a file (given with the command line) in the Controller or in a method in the Main class

Karatawi
  • 378
  • 2
  • 6
  • 16

1 Answers1

33

Try getParameters. This should give you the command line arguments

As wished a small example (i took the main code from Raphael's answer)

Assuming the controller class is named "MyController"

public class Main extends Application {

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

    FXMLLoader loader=new FXMLLoader(getClass().getResource("hive.fxml"));
    Parent root = loader.load();
    MyController cont=load.getController();
    /*
      This depends on your controller and you have to decide 
      How your controller need the arguments
    */
    cont.setParameter(getParameters()); 

    primaryStage.setTitle("Hive-viewer");
    primaryStage.setScene(new Scene(root, 1600, 900));
    primaryStage.show();
 }


 public static void main(String[] args) {
    launch(args);
 }
}
Emil S.
  • 412
  • 4
  • 20
Clayn
  • 1,016
  • 9
  • 11
  • 3
    It might be helpful to add some detail to this answer, with an example of how you would pass the parameter values retrieved from the method call to the controller. – James_D Mar 16 '16 at 13:05
  • 1
    @James_D Thanks for the suggestion. I hope its better now. I'm not so active on SO. – Clayn Mar 16 '16 at 13:39