SWT on JavaFX update-site available


Just for those who want to give SWT on JavaFX a try themselves you can now grab the nightly bits from http://download.eclipse.org/efxclipse/experimental-nightly/site_assembly.zip.

To develop a plain SWT/JavaFX app (no OSGi!) you should add the following jars:

  • org.eclipse.fx.runtime.swt_1.0.0.$buildtimestamp.jar: this is the real SWT implementation
  • org.eclipse.fx.runtime.swtutil_1.0.0.$buildtimestamp.jar: this is a set of utility classes who close the gap between things hard to implement in SWT on JavaFX like e.g. open a blocking shell – it will work on current SWT ports (win32,cocoa,gtk) and SWT on JavaFX
  • org.eclipse.fx.ui.controls_1.0.0.$buildtimestamp.jar: JavaFX controls implemented at e(fx)clipse and reused from the SWT port

and add them to your build-path.

A simple hello world application would look like this:

package sample;

import org.eclipse.fx.runtime.swtutil.SWTUtil;
import org.eclipse.fx.runtime.swtutil.SWTUtil.BlockCondition;
import org.eclipse.fx.runtime.swtutil.SWTUtil.SWTAppStart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class HelloWorldSWT implements SWTAppStart {

	@Override
	public BlockCondition createApp(Display display) {
		Shell shell = new Shell(display);
		shell.setText("Is it SWT or JavaFX?");
		shell.setLayout(new GridLayout());
		
		Button button = new Button(shell, SWT.PUSH);
		button.setText("Hello SWT on JavaFX");
		button.addListener(SWT.Selection, (e) -> System.out.println("SWT on JavaFX is great!"));
		button.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER, true, true));
		
		SWTUtil.getInstance().openBlocking(shell);
		
		return null;
	}

	public static void main(String[] args) {
		SWTUtil.getInstance().bootstrap(new HelloWorldSWT());
	}
}

(If you only use org.eclipse.fx.runtime.swtutil_1.0.0.$buildtimestamp.jar and the current stable SWT ports the above code will work equally!)

I think there are only 2 things that need explanation:

  • The bootstrapping or creation of the Display instance is abstracted because JavaFX requires to bootstrap applications in a very special way so creating a Display yourself is discouraged
  • Blocking until a condition is met e.g. a Shell is closed is. JavaFX does not directly expose the event loop. In SWT you stop the workflow of your application with code like this
    while(<condition>) {
      if(!display.readAndDispatch()) {
        display.sleep();
      }
    }
    

    Event loops are e.g. used to open a Shell and block until it is closed because the SWT-Shell only has a none blocking method named open(). JavaFX counter part to Shell is Stage and unlike Shell it has 2 methods show() and showAndWait() while SWT on JavaFX can emulate the SWT-Event-Loop spinning with the original API it is much more efficient to call out to showAndWait when you want to open a blocking shell.

Running the above code should open an application like this
helloworld

This entry was posted in e(fx)clipse. Bookmark the permalink.

2 Responses to SWT on JavaFX update-site available

  1. Pingback: Java desktop links of the week, April 28 « Jonathan Giles

  2. Pingback: JavaFX links of the week, April 28 // JavaFX News, Demos and Insight // FX Experience

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.