JavaFX – Passing Arguments


I received a query on how to pass arguments in JavaFX. It basically depends on how we deploy the application. The application can be deployed using JavaWebStart, as an applet or launched from command line.

Lets take a simple application..


var text = Text {
    x: 10, y: 30
    font : Font size : 16 }
    content: "Argument {FX.getArgument( "key" )}"
}

The argument is passed as key-value pair. The value is retrieved using FX.getArgument( <key> ) method. Arguments can be passed to various deployment modes as shown below.

Command Line:


javafx -cp FXArguments.jar fxarguments.Main key="From Command Line"

Applet:


<script src="http://dl.javafx.com/1.2/dtfx.js&#34;></script>
<script>
    javafx(
        {
              archive: "FXArguments.jar",
              draggable: true,
              width: 250,
              height: 80,
              code: "fxarguments.Main",
              name: "FXArguments"
        } , {
              key: "from Applet"
        }
    );
</script>

Launch Applet Example

Java Web Start:


<jnlp spec="1.0+" codebase="dist" href="FXArguments.jnlp">
    <application-desc main-class="com.sun.javafx.runtime.main.Main">
        <argument>MainJavaFXScript=fxarguments.Main</argument>
        <argument>key=from Java Web Start</argument>
    </application-desc>
</jnlp>

Launch Web Start Example

Source:

Advertisements

About Rakesh Menon
-

7 Responses to JavaFX – Passing Arguments

  1. Is it also possible to pass in an argument as a URL parameter for a web start application?
    Like this:
    http://www.test.com/app.jnlp?key=arg1&test=other
    /Pär

  2. Rakesh Menon says:

    @Pär Dahlberg That wont be possible. One way is to write a servlet with content-type as XML which will dynamically generate the jnlp file. This way we can pass the arguments to servlet which returns the required jnlp file.

  3. Hadn’t thought of that. Thanks 🙂
    /Pär

  4. LaoZhang says:

    i’m just looking for this, thanks a lot Rakesh!

  5. Rakesh Menon says:

    @LaoZhang Glad! Thanks!

  6. Sean C says:

    How do I pass jvm arguments to the applet? I’m running up against a problem with localization and cant’ figure how to translate this
    -Duser.language=fr -Duser.country=CA
    in to something that can be used by the applet. I’ve tried adding
    <param name="java_arguments" value="-Duser.language=fr -Duser.country=CA"/>
    inside the <applet-desc> tag inside the jnlp file, but no luck.
    thanks

  7. Rakesh Menon says:

    @Sean C You can use property tag inside resources in jnlp to set System properties.
    <property name="key" value="overwritten"/>
    There are some security restrictions for unsigned applets.
    Please refer to jnlp syntax for more info : http://java.sun.com/javase/6/docs/technotes/guides/javaws/developersguide/syntax.html#resources

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: