swingintegration.example
Class EmbeddedSwingComposite

java.lang.Object
  extended by org.eclipse.swt.widgets.Widget
      extended by org.eclipse.swt.widgets.Control
          extended by org.eclipse.swt.widgets.Scrollable
              extended by org.eclipse.swt.widgets.Composite
                  extended by swingintegration.example.EmbeddedSwingComposite
All Implemented Interfaces:
Drawable

public abstract class EmbeddedSwingComposite
extends Composite

A SWT composite widget for embedding Swing components in a SWT composite within an RCP or standalone-SWT application. The Eclipse platform provides limited support for embedding Swing components through SWT_AWT. This class extends that support by

If, rather than embedding Swing components, you are integrating with Swing by opening Swing dialogs, see the AwtEnvironment class.

This is an abstract that is normally used by extending it and implementing the createSwingComponent() method. For example,

        embeddedComposite = new EmbeddedSwingComposite(parent, SWT.NONE) {
            protected JComponent createSwingComponent() {
                scrollPane = new JScrollPane();
                table = new JTable();
                scrollPane.setViewportView(table);
                return scrollPane;
            }
        }; 
        embeddedComposite.populate();
 

The Swing component is created inside a standard Swing containment hierarchy, rooted in a RootPaneContainer. The root pane container is placed inside an AWT frame, as returned by SWT_AWT.new_Frame(Composite)

Note: When you mix components from Swing/AWT and SWT toolkits, there will be two UI event threads, one for AWT, one for SWT. Most SWT APIs require that you call them from the SWT thread. Swing has similar restrictions though it does not enforce them as much as SWT.

Applications need to be aware of the current thread, and, where necessary, schedule tasks to run on another thread. This has always been required in the pure Swing or SWT environments, but when mixing Swing and SWT, more of this scheduling will be necessary.

To schedule work on the AWT event thread, you can use:

(or similar methods in EventQueue)

To schedule work on the SWT event thread, use:

Of course, as in single-toolkit environments, long-running tasks should be offloaded from either UI thread to a background thread. The Eclipse jobs API can be used for this purpose.


Field Summary
 
Fields inherited from class org.eclipse.swt.widgets.Control
handle
 
Constructor Summary
EmbeddedSwingComposite(Composite parent, int style)
          Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.
 
Method Summary
protected  RootPaneContainer addRootPaneContainer(Frame frame)
          Adds a root pane container to the embedded AWT frame.
protected abstract  JComponent createSwingComponent()
          Creates the embedded Swing component.
 void dispose()
           
 boolean forceFocus()
           
 Frame getFrame()
          Returns the embedded AWT frame.
 void populate()
          Populates the embedded composite with the Swing component.
 boolean setFocus()
           
protected  void updateAwtFont(Font newFont)
          Performs custom updates to newly set fonts.
 
Methods inherited from class org.eclipse.swt.widgets.Composite
changed, checkSubclass, computeSize, getBackgroundMode, getChildren, getLayout, getLayoutDeferred, getTabList, isLayoutDeferred, layout, layout, layout, layout, setBackgroundMode, setLayout, setLayoutDeferred, setTabList
 
Methods inherited from class org.eclipse.swt.widgets.Scrollable
computeTrim, getClientArea, getHorizontalBar, getVerticalBar
 
Methods inherited from class org.eclipse.swt.widgets.Control
addControlListener, addFocusListener, addHelpListener, addKeyListener, addMouseListener, addMouseMoveListener, addMouseTrackListener, addPaintListener, addTraverseListener, computeSize, getAccessible, getBackground, getBackgroundImage, getBorderWidth, getBounds, getCursor, getEnabled, getFont, getForeground, getLayoutData, getLocation, getMenu, getMonitor, getParent, getShell, getSize, getToolTipText, getVisible, internal_dispose_GC, internal_new_GC, isEnabled, isFocusControl, isReparentable, isVisible, moveAbove, moveBelow, pack, pack, redraw, redraw, removeControlListener, removeFocusListener, removeHelpListener, removeKeyListener, removeMouseListener, removeMouseMoveListener, removeMouseTrackListener, removePaintListener, removeTraverseListener, setBackground, setBackgroundImage, setBounds, setBounds, setCapture, setCursor, setEnabled, setFont, setForeground, setLayoutData, setLocation, setLocation, setMenu, setParent, setRedraw, setSize, setSize, setToolTipText, setVisible, toControl, toControl, toDisplay, toDisplay, traverse, update
 
Methods inherited from class org.eclipse.swt.widgets.Widget
addDisposeListener, addListener, checkWidget, getData, getData, getDisplay, getStyle, isDisposed, isListening, notifyListeners, removeDisposeListener, removeListener, removeListener, setData, setData, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

EmbeddedSwingComposite

public EmbeddedSwingComposite(Composite parent,
                              int style)
Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

This method must be called from the SWT event thread.

The style value is either one of the style constants defined in class SWT which is applicable to instances of this class, or must be built by bitwise OR'ing together (that is, using the int "|" operator) two or more of those SWT style constants. The class description lists the style constants that are applicable to the class. Style bits are also inherited from superclasses.

The styles SWT.EMBEDDED and SWT.NO_BACKGROUND will be added to the specified style. Usually, no other style bits are needed.

Parameters:
parent - a widget which will be the parent of the new instance (cannot be null)
style - the style of widget to construct
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the parent is null
SWTException -
  • ERROR_THREAD_INVALID_ACCESS - if not called from the SWT event thread
See Also:
Widget.getStyle()
Method Detail

populate

public void populate()
Populates the embedded composite with the Swing component.

This method must be called from the SWT event thread.

The Swing component will be created by calling createSwingComponent(). The creation is scheduled asynchronously on the AWT event thread. This method does not wait for completion of this asynchronous task, so it may return before createSwingComponent() is complete.

The Swing component is created inside a standard Swing containment hierarchy, rooted in a RootPaneContainer. Clients can override addRootPaneContainer(Frame) to provide their own root pane container implementation.

This method can be called multiple times for a single instance. If an embedded frame exists from a previous call, it is disposed.

Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the SWT event thread

createSwingComponent

protected abstract JComponent createSwingComponent()
Creates the embedded Swing component. This method is called from the AWT event thread.

Implement this method to provide the Swing component that will be shown inside this composite. The returned component will be added to the Swing content pane. At least one component must be created by this method; null is not a valid return value.

Returns:
a non-null Swing component

addRootPaneContainer

protected RootPaneContainer addRootPaneContainer(Frame frame)
Adds a root pane container to the embedded AWT frame. Override this to provide your own RootPaneContainer implementation. In most cases, it is not necessary to override this method.

This method is called from the AWT event thread.

If you are defining your own root pane container, make sure that there is at least one heavyweight (AWT) component in the frame's containment hierarchy; otherwise, event processing will not work correctly. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4982522 for more information.

Parameters:
frame - the frame to which the root pane container is added
Returns:
a non-null Swing component

updateAwtFont

protected void updateAwtFont(Font newFont)
Performs custom updates to newly set fonts. This method is called whenever a change to the system font through the system settings (i.e. control panel) is detected.

This method is called from the AWT event thread.

In most cases it is not necessary to override this method. Normally, the implementation of this class will automatically propogate font changes to the embedded Swing components through Swing's Look and Feel support. However, if additional special processing is necessary, it can be done inside this method.

Parameters:
newFont - New AWT font

getFrame

public Frame getFrame()
Returns the embedded AWT frame. The returned frame is the root of the AWT containment hierarchy for the embedded Swing component. This method can be called from any thread.

Returns:
the embedded frame

setFocus

public boolean setFocus()
Overrides:
setFocus in class Composite

forceFocus

public boolean forceFocus()
Overrides:
forceFocus in class Control

dispose

public void dispose()
Overrides:
dispose in class Widget