Sunday, February 23, 2014

Get parameters/arguments in JavaFX application

When you start a JavaFX application, main() will not be called (I don't know the logic behind)! To Retrieves the parameters for this Application, including any arguments, we can call getParameters() method of javafx.application.Application class. getParameters() return a Application.Parameters. We can retrieve the parameters and arguments with its getNamed(), getRaw() or getUnnamed():

  • Map<String, String> getNamed():
    Retrieves a read-only map of the named parameters.
  • List<String> getRaw():
    Retrieves a read-only list of the raw arguments.
  • List<String> getUnnamed():
    Retrieves a read-only list of the unnamed parameters.
Example:

getParameters()
Example of using getParameters()


package javafx_hello;

import java.util.List;
import java.util.Map;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Hello extends Application {

    @Override
    public void init() throws Exception {
        super.init();
        
        System.out.println("init()");
        Parameters parameters = getParameters();
        
        Map<String, String> namedParameters = parameters.getNamed();
        List<String> rawArguments = parameters.getRaw();
        List<String> unnamedParameters = parameters.getUnnamed();
        
        System.out.println("\nnamedParameters -");
        for (Map.Entry<String,String> entry : namedParameters.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
        
        System.out.println("\nrawArguments -");
        for(String raw : rawArguments) {
            System.out.println(raw);
        }
        
        System.out.println("\nunnamedParameters -");
        for(String unnamed : unnamedParameters) {
            System.out.println(unnamed);
        }
    }
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        System.out.println("main()");
        launch(args);
    }
    
}

You can start the JavaFX application, JavaFX_Hello, in command line to pass named parameters with something like:
$ java -jar JavaFX_Hello.jar --width=320 --name=Java-Buddy

No comments:

Post a Comment