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
1
vote
0 answers

JTextField doesn't allow to enter text

I have a quite old plug-in for Eclipse which worked well until to Kepler. On Kepler the jTextFields don't allow to enter text. I reduced the code to the minimum trying figure out where is problem, but I stuck here. public class BeanDialog extends…
Fab
  • 1,468
  • 1
  • 16
  • 37
1
vote
1 answer

Adding SWT Canvas to JPanel

I'm trying to add a NatTable (which extends org.eclipse.swt.widgets.Canvas) to a JPanel (the majority of the program's graphics are in Swing, and I'm rather unfamiliar with SWT). I attempted to use the code below to test the SWT_AWT class, but I got…
1
vote
1 answer

Eclipse plugin - SWT_AWT not working on OSX

I'm developing an Eclipse plugin using the SWT_AWT bridge in order to design the gui using Swing. It works perfectly on Windows but when I try to run it in OSX i get this error: org.eclipse.swt.SWTError: Not implemented …
Oneiros
  • 4,328
  • 6
  • 40
  • 69
1
vote
3 answers

how to create a color palette inside a composite (swt)

i want to add a color palette inside a composite (swt). there is ColorPalette class but i don't know how to use it inside my composite. it will be inside eclipse view plugin
Dana Klein
  • 159
  • 1
  • 12
1
vote
6 answers

Swing Application Window not responding on Mac

I'm trying to get a basic Swing Application to run on my Mac OS X 10.8.2 (Java version 1.6.0_37) machine, and every time I try to run it from Eclipse, the frame appears, but I can't interact with it. I've tried to start from a basic, clean slate…
pgebhard
  • 59
  • 1
  • 10
0
votes
1 answer

ScrollPane causes my plugin to freeze

I use the following code to include an Applet in a ScrollPane into my view for my eclipse plugin: @Override public void createPartControl(Composite parent) { composite = new Composite(parent, SWT.EMBEDDED); final Frame frame =…
anon
0
votes
0 answers

Changing size of Swing JPanel inside of an Eclipse plugin's ViewPart (despite SwingUtilities.invokeLater(Runnable()))

Today, I am working on an Eclipse plugin project, where I am using the standard SWT_AWT bridge in order to plug my Swing components in: public class MyView extends ViewPart { public static final String ID = "HelloRCP.view"; Frame frame; …
0
votes
1 answer

How to get row data on right click in RCP

I am beginner of RCP framework. In this I have made one SWT table by using Table and Table item. In my table one check box and couple of progress bar and other row data is showing. I want to show couple of menu items on right click and on right…
Dum
  • 13
  • 3
0
votes
2 answers

Open URL in new browser with new session and cookies using SWT

I want to open an URL in the external browser with the new session and cookies for the URL. every time the URL is opened it shall be opened in a new browser with new cookies. My problem is, whenever I try to open the URL in new browser the old…
A.Jha
  • 15
  • 4
0
votes
1 answer

Access to an object of my SWT thread from my Swing thread

So I have an SWT thread, an eclipse plugin that consists of a simple view myView, with a JPanel myJPanel and a JTree myJTree embedded into it. With a listener on the Swing thread, when the selection change on my JTree it refresh JPanel into the…
Logan Wlv
  • 3,274
  • 5
  • 32
  • 54
0
votes
0 answers

User Selection getting lost in StyledText when fouse lost

I am using StyledText(org.eclipse.swt.custom) in UI. The problem currently i am facing is selection(Selected text in StyledText) is getting lost when focus moved away(EX:Some button clicked) from the StyledText. Is there any way to retain…
Ganesh Rao B
  • 461
  • 2
  • 8
  • 23
0
votes
1 answer

What is the performance hit from using SWT_AWT Bridge in Eclipse RCP

I am building an Eclipse RCP application that will eventually have section for NASA Worldwind AWT component. I will be using the SWT_AWT bridge to get the worldwind component embedded in an RCP application. I am trying to decide whether I should…
codecitrus
  • 659
  • 6
  • 17
0
votes
1 answer

Combining SWT and AWT/Swing: which GUI thread to take?

Working on a large scale SWT-based application I just stumbled upon some code using the AWT/Swing bridge which totally confused me and made me think about the implications of using two GUI threads. public void createContent(final String html) { …
0
votes
1 answer

How to filter values in Editable ComboBox or ComboViewer

how to filter the Values of Combo in SWT. For Examble if i have a,abc,bcd,acd,com,aaa,bed,bot,catvalues in combo dropdown. if i type "a" in combo it should display only "a,abc,acd,aaa" in combo dropdown. is there any way to get?
0
votes
1 answer

SWT background thread blocking the GUI

Below code is blocking my UI , I have checked other posts and this seems to be a correct implementation , Please let me know what I am doing wrong here: public class RecordTest { public static boolean stopFlag=false; public static void…
Amrit
  • 433
  • 3
  • 19