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 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
public class JavaFX_Hello extends Application {

    public void init() throws Exception {
        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("\nunnamedParameters -");
        for(String unnamed : unnamedParameters) {
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Hello World!");

    public static void main(String[] 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