Windows

io.Connect Windows

In order for windows created from an external Java apps to become io.Connect Windows, they must first be registered via the Java Window Management API.

Registering Swing Windows

The Window Management API is accessible through io.windows().

Currently, io.Connect Windows requires the underlying native handle (hwnd in the case of Windows) to be initialized and attached to the JFrame before the window can be registered. This is achieved internally through io.Connect Windows:

WindowHandle<JFrame> handle = windows.getWindowHandle(frame);
io.windows().register(handle)
        .thenAccept(window ->
                frame.addWindowListener(new WindowAdapter()
                {
                    @Override
                    public void windowClosing(WindowEvent e)
                    {
                        window.closeAsync();
                    }
                }));

Controlling the Window

Once an app window is registered, io.Connect Windows will accept full control over the window position, size and visibility. The app shouldn't use native methods (for example, Swing calls) to control the window as this will interfere with the io.Connect Window management.

The Java Window Management API allows you to control the following window properties:

Type

The window type is controlled by the mode window option which can be specified in the app definition or during window registration:

io.windows().register(handle, options -> options.mode(WindowMode.FLAT));

Title

The following example demonstrates how to set the window title during the window registration (this will ignore the title specified in the app definition):

io.windows().register(handle, options -> options.title("My Title"));

To change the window title at runtime, use the changeTitle() method of an io.Connect Window instance and pass the new title as an argument:

window.changeTitle("New Title");

Size & Position

To change the window bounds, use the changeBounds() method:

window.changeBounds(new Bounds(10, 10, 200, 200));

Visibility

To hide or show the window, use the changeVisibility() method. Changing the window visibility also affects its associated icon:

window.changeVisibility(false);

Frame Buttons

You can add extra buttons in the frame area of the window and handle clicks for them.

Adding Buttons

To add a new button to the window frame, use the addFrameButton() method:

window.addFrameButton("search-button",
                      ButtonOptions.builder()
                              .toolTip("Search")
                              .order(1)
                              .image(new byte[0]) // needs to be a valid image
                              .build())
        .thenRun(() -> System.out.println("created button"));

Removing Buttons

To remove a button from the window frame, use the removeFrameButton() method:

window.removeFrameButton("search-button")
        .thenRun(() -> System.out.println("removed button"));

Window Events

The Java Window Management API offers methods for handling io.Connect Window events related to changes of the window title, bounds, visibility, context and more.

Title

To subscribe for changes of the window title, use the onTitleChanged() method:

window.onTitleChanged(e -> System.out.println("Window title changed to: " + e.getTitle()));

Size & Position

To subscribe for changes of the window bounds, use the onBoundsChanged() method:

window.onBoundsChanged(e -> System.out.println("Window bounds changed to: " + e.getBounds()));

Visibility

To subscribe for changes of the window visibility, use the onVisibilityChanged() method:

window.onVisibilityChanged(e -> System.out.println("Window is now " + (e.isVisible() ? "visible." : "hidden.")));

Focus

To subscribe for changes of the window focus, use the onFocusChanged() method:

window.onFocusChanged(e -> System.out.println("Window " + (e.isFocused() ? "is now on focus." : "has lost focus.")));

Context

To subscribe for updates of the window context, use the onContextUpdated() method:

window.onContextUpdated(e -> System.out.println("Window context udpated: " + e.getContext()));

Frame Buttons

To subscribe for clicks on any frame buttons you may have added to the window, use the onFrameButtonClicked() method:

window.onFrameButtonClicked(e -> {
    if ("search-button".equals(e.getButtonId())) {
        System.out.println("Search button clicked");
    }
});

Close

To prevent an io.Connect Window from closing, use the onClosing() method. The event handler callback must return asynchronously a PreventClosingOptions object whose methods you can use to specify the behavior of a window that is about to be closed.

The following example demonstrates how to prevent the window from closing and show a confirmation dialog to the user:

window.onClosing(() → CompletableFuture.supplyAsync(PreventClosingOptions :: dialog));

The PreventClosingOptions object has the following methods:

Method Description
allow() Will allow the window to close.
dialog() Will prevent the window from closing and will show a confirmation dialog to the user.
prevent() Will prevent the window from closing.