Questions tagged [swt-awt]

SWT-AWT refers to the bridge that makes mixing SWT and SWING/AWT graphical components in a single application possible. Use this tag for questions regarding communication and/or cohabitation between the two technologies.

stands for a bridge developed with a goal to make SWING and SWT applications coexist in the same application.

Introduction

Swing and SWT are sometimes seen as strictly competing technologies. Some people have strong opinions on which UI toolkit to use exclusively for client applications. However, in the real world, ideological extremes are often impractical. Some valid use cases require both technologies to coexist in a single application. While mixing the two toolkits is not a simple task, it can be done, and it can be done such that the two toolkits are smoothly integrated. This article discusses the steps necessary to achieve good Swing/SWT integration. It focuses on the use case of embedding existing Swing components into an SWT-based Rich Client Platform application.
(By Gordon Hirsch, SAS Institute Inc. Copyright © 2007 SAS Institute Inc. Made available under the EPL v1.0 June 20, 2007)

Using the SWT/AWT Bridge

The SWT/AWT Bridge has been part of SWT since version 3.0. It is a very simple API, located in the package org.eclipse.swt.awt. [Note]

This article focuses on embedding AWT frames inside SWT composites. It demonstrates only one half of the SWT/AWT Bridge. Nevertheless, most of the improvements described below also apply to the other direction: embedding SWT composites inside AWT frames.

Minimally, embedding an AWT frame inside an SWT composite is just two simple lines of code

Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
Frame frame = SWT_AWT.new_Frame(composite);

An instance of org.eclipse.swt.Composite is created with the SWT.EMBEDDED style. This style signals that an AWT frame is to be embedded inside the Composite. The call to the static new_Frame method creates and returns such a frame. The frame may then be populated with AWT and/or Swing components.

The returned frame is not a standard AWT frame. By default, it is a subclass of java.awt.Frame that is meant to be embedded within native applications. In fact, it is the same frame that is used to embed applets inside a browser window.

The example code shown above is deceptively simple. While SWT does much under the covers to manage the integration of the two toolkits, the scope of the bridge's implementation is very narrow. In reality, you must do much more in your application to make the integration more consistent

Multiple Event Threads

Swing/SWT integration has important threading implications. Each UI toolkit has its own event queue, and each event queue is processed by a separate thread. Most SWT APIs must be called from the SWT event thread. Swing has similar restrictions though they are not as strictly enforced. This split is the major drawback of mixing the toolkits, and it adds some complexity to the code.

Applications must be aware of the current thread, and, where necessary, schedule tasks to run on the appropriate UI toolkit thread. To schedule work on the AWT event thread, use:

javax.swing.SwingUtilities.invokeLater()
javax.swing.SwingUtilities.invokeAndWait() 

To schedule work on the SWT event thread, use:

org.eclipse.swt.widgets.Display.asyncExec()
org.eclipse.swt.widgets.Display.syncExec() 

These are the same APIs used in a single-toolkit environment to keep the UI responsive while offloading long running operations to a worker thread. With Swing/SWT integration they are used for the additional purpose of moving work from one event thread to another.

The use of multiple event threads increases the risk of deadlock. Whenever possible, try to avoid blocking one event thread while scheduling work on the other event thread. In other words, avoid calling SwingUtilities.invokeAndWait from the SWT event thread and avoid calling Display.syncExec from the AWT event thread. Otherwise, if there's ever a case where one blocking call is made while the other thread has made its own blocking call in the other direction, deadlock will occur.

References

FAQ How do I embed AWT and Swing inside SWT

37 questions
19
votes
1 answer

Making composite focusable in SWT

Is it possible to create a focusable composite in SWT? I'm catching all keyboard events via Display filter, but there are some problems when the focus is on the tree or list - GTK+'s default action is to search in the contents of the control. What I…
m4tx
  • 4,139
  • 5
  • 37
  • 61
5
votes
2 answers

Java SWT : Badge Notifications

I have a desktop based UI application written in Java SWT running on windows. I want to add a button on the UI screen whose behaviour should be similar to the badges on an iphone or facebook notifications as shown in the images below. The number on…
Dunxton
  • 408
  • 1
  • 8
  • 21
3
votes
1 answer

Update swt plugins on eclipse 3.7.2

I have an Eclipse 3.7.2 RCP application and I need to run this with JDK 7 on Mac OSX. I need SWT_AWT bridge fixes from SWT 4.3. Is there a way I can update to latest SWT in eclipse 3.7.2. The SWT binaries available here are not plugins.
Viral
  • 245
  • 1
  • 2
  • 8
3
votes
1 answer

SWT Browser - Swing Integration - Mac - JDK 1.7

Right, so Ive got an interesting problem here concerning SWT and swing integration on mac running java 1.7. Im trying to embed an SWT Browser widget into my swing project as a panel which is pretty simple to do on java version 1.6. There has been a…
Dylan Vorster
  • 56
  • 1
  • 6
2
votes
1 answer

MouseWheel event doesn't fire in SWT-AWT component

I am stuck at mousewheel event that just doesn't fire. I use swt_awt bridge, so I am able to use swing component in my RCP application. I tested everything I was able to find, but with no success. Because my application is awfully complex, I created…
Durin
  • 93
  • 6
2
votes
0 answers

SWT/AWT integration: alternatives to or further development of Albireo

There is quite a lot of manual work needed to improve SWT/Swing integration when using SWT_AWT bridge. Albireo project was intended to fix this, but it's now archived. Is there any alternative? Or is it developed further unofficially?
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
2
votes
2 answers

Refreshing Eclipse RCP part containing an AWT-SWT bridge when contents of the bridge change

I'm working on an RCP application that's in a transition from a Swing version. So we have a lot of UI components that still need to live in the Swing world during this transition. I'm able to properly place the existing Swing components in AWT-SWT…
anishthecoder
  • 940
  • 6
  • 20
2
votes
0 answers

Hang on Mac with zoomed desktop when starting an app. with both Swing & SWT

I am on a Mac ( tested it with 10.6.4, 10.7.5 & 10.8.2 ) with my desktop zoomed. When lunching my Java application that use both Swing and SWT the window is freezing right at the opening. The issue can also be reproduced with the snippet 337…
2
votes
0 answers

ViewPart activation broken with jre7 and SWT_AWT

I am using the SWT_AWT bridge in my RCP application with jre6 and it works just fine. The setup code is taken from the Eclipse Corner Article: Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND); Frame frame =…
msteiger
  • 2,024
  • 16
  • 22
1
vote
1 answer

Showing SWT modal dialog from AWT/Swing

Using Albireo, it's easy to see how to show a Swing dialog from SWT: private AwtEnvironment awtEnv = AwtEnvironment.getInstance(Display.getCurrent); ... // call from SWT thread void showSwingMessageDialog(String msg) { …
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
1
vote
2 answers

JTree does not extend automatically, Swing embedded into SWT

So I am working with Swing components embedded into SWT. Here a simplified version of my issue, basically I have two Panel objects (panelA, panelB) with some Swing components, embedded in my Shell. I want to add an on/off option on the panelB,…
Logan Wlv
  • 3,274
  • 5
  • 32
  • 54
1
vote
0 answers

JxBrowser didn't work on Mac

I am trying to use JxBrowser in Eclipse plugin by following https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013072-using-jxbrowser-in-swt. Here's part of my code: import org.eclipse.swt.SWT; import…
Dragon
  • 487
  • 4
  • 17
1
vote
1 answer

Testing SWT application using SWTBot:

Testing SWT application with SWTBot: I came across one requirement, where I need to test an SWT application using SWTBot. I had no idea how to start with SWTBot, after referring few blogs I can setup the SWTBot using eclipse. And also I found that…
Anand
  • 11
  • 1
1
vote
1 answer

How to Programatically resize Detached Viewpart Window in Eclipse RCP?

I have a Viewpart in Eclipse RCP which I am detaching using inbuild method. Now I have a button on that Detached ViewPart and Upon Clicking it window Size of Windows (in Which detached Viewpart is there) should increase but I can't find any API for…
spt025
  • 2,134
  • 2
  • 20
  • 26
1
vote
0 answers

SWT_AWT Double Buffer Issue

I have an Eclipse RCP (SWT) Plugin Project and have a view part that has an SWT_AWT Frame with a JTree/JScrollPane/Etc using the following code: @PostConstruct public void postConstruct(Composite parent) { parent.setLayout(new…
Mister
  • 487
  • 3
  • 17
1
2 3