Welcome to OpenFin Docs

Explore our guides, resources and references for building with OpenFin

Get Started

Java adapter

Setup

Install the latest version of the OpenFin Desktop Java Adapter.
We provide an OpenFin.Desktop namespace (import com.openfin.desktop.*;) that can be used to connect to the desktop:

final DesktopConnection desktopConnection = new DesktopConnection("Unique UUID");
final RuntimeConfiguration rcg = new RuntimeConfiguration();
final int timeout = 60;
 
rcg.setRuntimeVersion("stable");
desktopConnection.connect(rcg, new DesktopStateListener() { 
  // your implementation
}, timeout);

The DesktopStateListener will let you react to changes to your connection to the desktop

new DesktopStateListener() {
  @Override
  public void onReady() {
  }

  @Override
  public void onClose(String error) {
  }

  @Override
  public void onError(String reason) {                    
  }

  @Override
  public void onMessage(String message) {
  }

  @Override
  public void onOutgoingMessage(String message) {
  }
}

Usage

Inter-Appliction Bus

Firstly we need to get the instance of the OpenFin desktop connection created earlier.
In this example we want to pass a JSON object (we can also pass just a string) by importing e.g. import org.json.JSONObject;

If all you need is to Publish and Subscribe to messages hosted on the OpenFin desktop, then you can use the following code to dispatch a message on a topic which will be picked up by any Subscribers to the same topic.

JSONObject msg = new JSONObject();
msg.put("name", "Java Demo");
msg.put("text", "Message");

desktopConnection.getInterApplicationBus().publish("ATopic", msg);

To subscribe to messages from other applications you can do the following.

desktopConnection.getInterApplicationBus().subscribe("*", "ATopic", (String sourceUuid, String receivingTopic, Object payload) -> {
  try {
    JSONObject message = (JSONObject) payload;
    String name = message.getString("name");
    System.out.println("Name received: " +  name);
  } catch (Exception e) {
    System.out.println("Error: " +  e.getMessage());
  }
});

Channel API

OpenFin’s Channel API allows you finer grained control over your messaging. With it we can create channels, and clients of those channels. Each channel can “register” a list of commands/actions that the channel provider will handle.

Firstly we need to get the instance of the OpenFin desktop connection created earlier.

Next, let's create a channel called “MyAppActions” and register a sayHello function.

desktopConnection.getChannel("MyAppActions").create(new AsyncCallback<ChannelProvider>() {
            @Override
            public void onSuccess(ChannelProvider provider) {
                //provider created, register actions.
           
                provider.register("sayHello", new ChannelAction() {
                    @Override
                    public JSONObject invoke(String action, JSONObject payload, JSONObject senderIdentity) {
                        
                        String name = payload.getString("name")
                        JSONObject obj = new JSONObject();
                        obj.put("message", "Hi " + name);
                        return obj;
                    }
                });
            }
        });

Then we can create a client of the channel we created above and call the sayHello function.

desktopConnection.getChannel("MyAppActions").connect(new AsyncCallback<ChannelClient>() {
            @Override
            public void onSuccess(ChannelClient client) {
                //connected to provider, invoke action provided by the provider.
                JSONObject payload = new JSONObject();
                payload.put("name", "John");
                client.dispatch("sayHello", payload, new AckListener() {
                    @Override
                    public void onSuccess(Ack ack) {
                        logger.info("current value={}", ack.getJsonObject().getJSONObject("data").getJSONObject("result").getString("message"));
                    }

                    @Override
                    public void onError(Ack ack) {
                    }
                });
            }
        });

Embedded View

Create a canvas and place it where the HTML5 application should be embedded.

embedCanvas = new java.awt.Canvas();
panel.add(embedCanvas, BorderLayout.CENTER);

Listen to the canvas resize event, and resize embedded HTML5 application accordingly.

	embedCanvas.addComponentListener(new ComponentAdapter() {
	    @Override
	    public void componentResized(ComponentEvent event) {
	        super.componentResized(event);
	        Dimension newSize = event.getComponent().getSize();
	        try {
	            if (startupHtml5app != null) {
	                startupHtml5app.getWindow().embedComponentSizeChange((int)newSize.getWidth(), (int)newSize.getHeight());
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	});

Launch and connect to OpenFin runtime

this.desktopConnection = new DesktopConnection(appUuid);
DesktopStateListener listener = new DesktopStateListener() {...};
RuntimeConfiguration configuration = new RuntimeConfiguration();
configuration.setRuntimeVersion(desktopVersion);
desktopConnection.connect(configuration, listener, 60);

Create HTML5 application

ApplicationOptions options = new ApplicationOptions(startupUuid, startupUuid, openfin_app_url);
WindowOptions mainWindowOptions = new WindowOptions();
options.setMainWindowOptions(mainWindowOptions);
DemoUtils.runApplication(options, this.desktopConnection, new AckListener() {...});

Embed HTML5 application into the canvas

startupHtml5app = Application.wrap(this.startupUuid, this.desktopConnection);
Window html5Wnd = startupHtml5app.getWindow();
long parentHWndId = Native.getComponentID(this.embedCanvas);
html5Wnd.embedInto(parentHWndId, this.embedCanvas.getWidth(), this.embedCanvas.getHeight(), new AckListener() {...});

Notifications

Notifications requires importing the following:

import com.openfin.desktop.notifications.*;
import com.openfin.desktop.notifications.NotificationOptions;
import com.openfin.desktop.notifications.events.NotificationActionEvent;

Add an event listener to respond to notification interactions. You will need to pass the desktop connection created earlier:

Notifications notifications = new Notifications(desktopConnection);
notifications.addEventListener(Notifications.EVENT_TYPE_ACTION, ne ->{
            NotificationActionEvent actionEvent = (NotificationActionEvent) ne;
            NotificationActionResult actionResult = actionEvent.getResult();
            java.lang.System.out.println("actionResult: notificationId: " + actionEvent.getNotificationOptions().getId() + ", user clicked on btn: " + actionResult.getString("btn"));
        });

Once you have a listener set up you can create a notification:

NotificationOptions opt = new NotificationOptions("Notification from Java", "Write once, run everywhere", "Category");

opt.setSticky(NotificationOptions.STICKY_STICKY);
opt.setIndicator(new NotificationIndicator(NotificationIndicator.TYPE_SUCCESS));
        
ButtonOptions bo1 = new ButtonOptions("Button 1");
bo1.setOnClick(new NotificationActionResult(new JSONObject().put("btn", "btn1")));
        
ButtonOptions bo2 = new ButtonOptions("Button 2");
bo2.setOnClick(new NotificationActionResult(new JSONObject().put("btn", "btn2")));
bo2.setCta(true);
        
opt.setButtons(bo1, bo2);

notifications.create(opt);

To see the different types of notifications we recommend using our notification generator tool which will let you easily experiment with the settings available.

License

Please include your licenseKey in RuntimeConfiguration.setLicenseKey when launching apps from the Java Adapter. If interested in an Enterprise license, please contact us for one here.

Updated 24 days ago


Java adapter


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.