SWT Extension for Vaadin SwingBridge - Vaadin Add-on Directory
SWT extension for SwingBridgeThis add-on runs an existing SWT application in the browser as part of a Vaadin Flow application, without rewriting it. It builds on [Vaadin SwingBridge](https://vaadin.com/docs/latest/tools/modernization-toolkit/swing-bridge): you embed the SWT application in a Vaadin view with a single component, and each user gets a separate instance of the application on the server. Vaadin views and the SWT application can communicate in both directions, so you can modernize the application one screen at a time.
**Requirements:** Vaadin SwingBridge and a SwingBridge license. As SWT uses the operating system's own widgets, the server currently has to run on Windows.
### Features
- **Run SWT in the browser:** embed an existing SWT application, with its native Win32 widgets, in a Vaadin Flow view.
- **Isolated user sessions:** every user gets a separate instance of the SWT application, running on the server.
- **Bidirectional communication:** call methods between Vaadin views and the SWT application using SwingBridge annotations and generated interfaces.
- **Gradual modernization:** replace SWT screens with Vaadin views one at a time.
### Getting Started
The [starter project](https://github.com/vaadin-component-factory/skeleton-starter-vaadin-swing-bridge-swt) is a runnable Vaadin application with an embedded SWT example. On Windows, clone and run it:
```bat
git clone https://github.com/vaadin-component-factory/skeleton-starter-vaadin-swing-bridge-swt.git
cd skeleton-starter-vaadin-swing-bridge-swt
start-vaadin.bat
```
Then open http://localhost:8888 and sign in with `admin` / `admin`.
### Embed Your SWT Application
1. Place your application's JAR files in the `applibs/` directory. Do not include the SWT libraries.
2. Add the `SwtBridge` component to a Vaadin view. The application shell must have `@Push` enabled.
```java
@Route("")
public class MyAppView extends VerticalLayout {
public MyAppView() {
setSizeFull();
SwtBridge app = new SwtBridge("com.example.MyApp");
add(app);
expand(app);
}
}
```
3. Run the application. The starter's `pom.xml` includes the required dependencies and copies SWT and JNA where the extension loads them.
### Communication Between Vaadin and SWT
The extension uses SwingBridge's interoperability features in both directions.
### Call SWT methods from Vaadin
Annotate the methods of the SWT application that Vaadin may call:
```java
public final class BridgeApp {
@InstanceProvider
public static BridgeApp current() { /* ... */ }
@ExposedMethod
public void showProducts() { /* ... */ }
}
```
SwingBridge's `swing-bridge-codegen-maven-plugin` generates a type-safe interface from the JARs in `applibs/`, which Vaadin code calls:
```java
SwtBridge.interop("com.example.BridgeApp")
.of(BridgeAppBridge.class)
.onReady(BridgeAppBridge::showProducts);
```
### Call Vaadin methods from SWT
The SWT application's listeners can invoke methods in Vaadin views annotated with `@VaadinCallback`.
### Learn More
- [WarehousePro SWT Demo](https://github.com/onuridrisoglu/swt-bridge-demo): an SWT desktop application running in the browser, talking to Vaadin views, and modernized screen by screen.
- [Vaadin SwingBridge Documentation](https://vaadin.com/docs/latest/tools/modernization-toolkit/swing-bridge)
### Sponsored Development
Major parts of this add-on have been sponsored by customers of Vaadin. Read more about Expert on Demand at: [Support](https://vaadin.com/support) and [Pricing](https://vaadin.com/pricing).