Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the args in Application.launch(Class, String... args)

I am invoking a JavaFX application from java.I want to use the String arguments in that JavaFX application. How to get that parameters in ChatWithSpecificClient?

For example :

Invoking Class

public class GenWindow{

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

Invoked Class

public class ChatWithSpecificClient extends Application {

    private Parent createScene() {
        BorderPane pane = new BorderPane();

        return pane;
    }

    @Override
    public void start(Stage primaryStage) {
        Scene scene = new Scene(createScene());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

For instance, How to set the title of this window to that argument?

like image 424
CodeRunner Avatar asked Feb 28 '16 07:02

CodeRunner


2 Answers

By using getRaw() method of the Parameters class, you can get a String list of arguments passed to the launch method of Application class. For example if you, invoke the application as.

Application.launch(ChatWithSpecificClient.class, "Client's name", "email");

Then at the end of the start(Stage s) method of your class get these values as a String list.

Parameters params = getParameters();
List<String> list = params.getRaw();
System.out.println(list.size());
for(String each : list){
    System.out.println(each);
}
like image 87
Muhammad Avatar answered Oct 14 '22 16:10

Muhammad


From the documentation:

args - the command line arguments passed to the application. An application may get these parameters using the getParameters() method.

like image 40
Itai Avatar answered Oct 14 '22 16:10

Itai