JavaFX - Adding extra starting parameters

Tags: java javafx
Question!

I worked with JavaFX because I had to programm an "interactive shell". I already programmed the Class Shell which connects to the remote host via SSH.

But the Shell has three parameters in his constructor:

public Shell(String username, String password, String host) {

    this.username = username;
    this.password = password;
    this.host     = host;
}

And what I would need (or would be perfect):

@Override
public void start(Stage primaryStage,String username, String password, String host) {
    this.primaryStage = primaryStage;
    this.shell = new Shell(username,password,host);
    initialiseOverview();
}

Is there any way I could add extra/optional starting Parameter to the JavaFX start method. Or is there any other way how I could handle this problem?

Thank you in advance :)



Answers

You can access the command line parameters with Application.getParameters():

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    Application.Parameters parameters = getParameters();
    List<String> rawParams = parameters.getRaw();
    String userName = rawParams.get(0);
    String password = rawParams.get(1);
    String host = rawParams.get(2);
    this.shell = new Shell(username,password,host);
    initialiseOverview();
}

This code assumes there are (at least) three command line parameters. You probably want to add checks for the number of parameters and show an error message or prompt if they are missing, etc.

By : James_D


Your system clock is probably more than 5 minutes off.

By : Jonas


There is a way if the first element is the only one to start with that character:

$ a=(asd fgh ijk)
$ echo "${a[@]^[!${a:0:1}]}"
asd Fgh Ijk

This extracts the very first character of the first element, then excludes that character from being uppercased; [!${a:0:1}] expands to [!a], which matches only f and i as the first characters of their elements.

It fails if multiple elements start with the same character, though:

$ a=(asd agh ijk)
$ echo "${a[@]^[!${a:0:1}]}"
asd agh Ijk


This video can help you solving your question :)
By: admin