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

16 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:

16.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 16-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 16-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.

16.2 Accessing Application Parameters

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

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

Example 16-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.

16.3 Using JavaFX Host Services

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

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

Example 16-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/");
    }
});

16.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 16-4.

Example 16-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.

16.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 16-5.

Example 16-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());
    }
}
Contents    Previous    Next

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