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:
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.
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.
For JavaFX applications, the Application.getHostServices()
method provides access to execution-mode-specific services, including:
Access to information about the code base and the document base.
For example, for embedded applications this is the URL of the application and URL of the host web page, respectively.
Access to the host web page using the JavaScript engine, only available to embedded applications.
Ability to open a web page in the browser.
Example 18-3 shows a few things you can do with getHostServices()
.
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.
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.