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.
Example 18-1 Detect if the JavaFX Application is Embedded in a Web Page
public void start(Stage stage) { boolean isEmbedded = (stage.getWidth() > 0); ... }
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:
Example 18-2 Get a List of Named Deployment Parameters and Values
Map m = getParameters().getNamed(); String labelText = "List of application parameters: \n"; for(String st: (Set<String>) m.keySet()) { labelText += " ["+st+"] : ["+m.get(st)+"]\n"; }
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()
.
Example 18-3 Using getHostServices()
final HostServices services = getHostServices(); Button jsButton = new Button("Test Javascript"); jsButton.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent t) { JSObject js = services.getWebContext(); js.eval("window.alert('Hello from JavaFX')"); } }); Button openButton = new Button("Test openDocument()"); openButton.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent t) { services.showDocument("http://javafx.com/"); } });
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.
Example 18-4 Use the getResource() Method to Load Resources
scene.getStylesheets(). add(this.getClass().getResource("my.css").toExternalForm());
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.
Example 18-5 Using Listeners to Resize an Embedded Application
public class ResizeFriendlyApp extends Application implements ChangeListener<Number> { private Stage stage; public void start(Stage stage) { //be resize friendly this.stage = stage; stage.widthProperty().addListener(this); stage.heightProperty().addListener(this); ... //resize content resize(stage.getWidth(), stage.getHeight()); stage.show(); } private void resize(double width, double height) { //relayout the application to match given size } public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) { resize(stage.getWidth(), stage.getHeight()); } }