Documentation Contents
Java Platform, Standard Edition Deployment Guide
Contents    Previous    Next

18 Coding Tips

This topic provides programming tips for development and deployment of Java and JavaFX applications that work in all execution environments.

This topic includes the following sections:

18.1 Detecting Embedded Applications

When an application is run in a browser, the application gets staged with predefined dimensions, which cannot be updated directly. Example 18-1 shows simple code to detect if a JavaFX application is embedded in a web page. The code can be used in either the main application or the JavaFX preloader start method.

As an alternative, you can try to get a reference to the web context from the Application.getHostServices() method. Null is returned if the application is not embedded.

18.2 Accessing Application Parameters

JavaFX applications support both named and unnamed parameters that can be passed in a variety of ways:

  • Specified on the command line for a standalone launch.

  • Hardcoded in the application package (JAR and deployment descriptor).

  • Passed from the HTML page in which the application is embedded.

In a JavaFX application, access parameters from a preloader or main application using the getParameters() method. For example, the code in Example 18-2 gets a list of all named parameters and their values:

For Java applets, see Defining and Using Applet Parameters in the Java Tutorials for information on accessing application parameters.

18.3 Using JavaFX Host Services

For JavaFX applications, the Application.getHostServices() method provides access to execution-mode-specific services, including:

Example 18-3 shows a few things you can do with getHostServices().

18.4 Loading Resources

Using the File API and explicit relative references to external data files or resources might not work when the application is loaded from the web.

To refer to resources relative to your application, use the getResource() method on one of the application classes, as shown in Example 18-4.

As an alternative, consider using getCodeBase() or getDocumentBase() from the HostServices class to refer to resources relative to the application or the location where the application is used.

18.5 Managing the Stage Size of JavaFX Applications

When a JavaFX application is embedded in a web page, the application cannot control stage dimensions. Dimensions specified at packaging time are preferences only and can be overridden by the user, for example if the user has custom browser zoom settings. Also, the stage can be resized at any time by the user.

To provide a good user experience, be prepared for arbitrary stage sizes. Otherwise, the application might be cropped, or garbage could be painted in the unused area of the stage.

If your application uses layouts, then you do not need to do anything. Layouts take care of resizing for you. Otherwise, implement resizing logic and listen to stage dimension changes to update the application, as shown in the simplified code in Example 18-5.

Contents    Previous    Next

Oracle and/or its affiliates Copyright © 1993, 2017, Oracle and/or its affiliates. All rights reserved.
Contact Us