Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

GEF/GEF4/SwtFX

Note to non-wiki readers: This documentation is generated from the Eclipse wiki - if you have corrections or additions it would be awesome if you added them in the original wiki page.

Introduction

The GEF4 SwtFX component provides specific JavaFX Node implementations which embed SWT Controls in a JavaFX scene graph. The component is subdivided into different packages:

  • org.eclipse.gef4.swtfx
    Contains SWT specific sub-classes of JavaFX framework classes to make embedding SWT Controls in a JavaFX scene graph possible.
  • org.eclipse.gef4.swtfx.controls
    Contains a generic JavaFX adapter Node implementation to integrate SWT controls into a JavaFX scene.

General

  • package: org.eclipse.gef4.swtfx

In order to be able to embed SWT Controls in a JavaFX scene graph, we decided to sub-class two JavaFX framework classes: FXCanvas, and Scene. This is due to the API mismatch of SWT and JavaFX. To create an SWT Control, you need to know its parent (a Composite). But in JavaFX you can create all the nodes you need first, and then plug them together to the full scene graph. Therefore, a JavaFX Node for an SWT Control, which we will refer to as SwtFXControl, has to create the "peer" SWT Control not until it can acquire an SWT Composite. For the embedding of JavaFX in an SWT application, JavaFX provides the FXCanvas (which is a SWT Composite). Our SwtFXScene allows access to a corresponding SwtFXCanvas, so that a SwtFXControl can check for its Scene to determine if creating the peer is possible.

This is how you create the context for SwtFX:

import org.eclipse.gef4.swtfx.SwtFXCanvas;
import org.eclipse.gef4.swtfx.SwtFXScene;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
public abstract class SwtFXApplication {
	public SwtFXApplication() {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		SwtFXCanvas canvas = new SwtFXCanvas(shell, SWT.NONE);
 
		SwtFXScene scene = createScene();
		canvas.setScene(scene);
 
		shell.setSize((int) scene.getWidth(), (int) scene.getHeight());
		shell.open();
 
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
 
	public abstract SwtFXScene createScene();
}

Back to the top