Home
.. Links
.. Search
.. Plugins
.. Help
.. Irc Faq

Projects
.. Platform/Faq
.. JDT/Faq/Plan
.. PDE/Faq
.. SWT/Faq
.. RCP/Faq

Tools Projects
.. CDT/Faq
.. GEF/Faq
.. EMF/Faq

Wiki Tutorials

Hosted Projects
.. MTJ
.. Google Summer Of Code 2007
.. Update Manager 2.0
.. EasyEclipse
.. Stylebase for Eclipse

Archives

AWTBridgeExample


import java.awt.Color;
import java.awt.Frame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

//SWT_AWT Bridge example.
//
// Omry Yadan.
//   Updated at 17/8/2005
public class Test
{
    public static void main(String[] args)
    {

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Main");
        shell.setBounds(0, 0, 200, 250);

        Composite swt = new Composite(shell, SWT.NONE);
        Label swtLabel = new Label(swt, SWT.NONE);
        swtLabel.setText("swt label");
        swtLabel.setBounds(10, 10, 70, 20);
        swt.setBounds(0, 0, 200, 250);
        swt.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

        Composite SWT_AWT_container = new Composite(swt, SWT.EMBEDDED);
        SWT_AWT_container.setBounds(0, 50, 200, 150);

        Frame awt = SWT_AWT.new_Frame(SWT_AWT_container);
        awt.setBackground(Color.red);
        awt.setBounds(0, 0, 200, 150);

        java.awt.Label awtLabel = new java.awt.Label("AWT Label");
        awtLabel.setBounds(0, 0, 70, 20);
        awt.add(awtLabel);
        shell.open();

        while (!shell.isDisposed())
        {

            if (!display.readAndDispatch())
            {

                display.sleep();

            }

        }

        display.dispose();

    }
}

 

Note also that starting with Eclipse 3.2M1, a new method is available to retrieve the embedded  AWT Frame in a Composite following this enhancement request.

The method is SWT_AWT.getFrame(Composite).  It returns the embedded for a Composite, if there is one. A patch is also attached that provides the same feature (albeit in a somewhat fragile way) for Eclipse 3.x, and that can be implemented as a static method in any class.


Comments:

From ania - 7/18/05 2:12 AM

sorry - simple mistake - there supposed to be 'swt library' in the first line ofthe previous post

From ania - 7/18/05 2:11 AM

I just wanted to complain about my version of awt library. I have no new_Panel AND new_Frame implemented in the swt package. All examples for applets using SWT that I found in the Internet were using at least one of those classes. Is there any other way to build such an applet? Otherwise, where can I find version of SWT that has those classes?

From exquisitus - 5/23/05 5:32 AM

The code sample does not compile as it is using method new_Panel, which does not exist in SWT_AWT class. Here is a working version: public class Main { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Main"); shell.setBounds(0, 0, 200, 250); Composite swt = new Composite(shell, SWT.NONE); Label swtLabel = new Label(swt, SWT.NONE); swtLabel.setText("swt label"); swtLabel.setBounds(10, 10, 70, 20); swt.setBounds(0, 0, 200, 250); swt.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); Composite SWT_AWT_container = new Composite(swt, SWT.EMBEDDED); SWT_AWT_container.setBounds(0, 50, 200, 150); Panel awt = new Panel(); Frame awtFrame = SWT_AWT.new_Frame(SWT_AWT_container); awtFrame.add(awt); awt.setBackground(Color.red); awt.setBounds(0, 0, 200, 150); java.awt.Label awtLabel = new java.awt.Label("AWT Label"); awtLabel.setBounds(0, 0, 70, 20); awt.add(awtLabel); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }

From youkenkin - 10/17/04 9:32 PM

how can i use swt in my applet? can you help me?
youkenkin@t-xcs.com


Last Modified 6/21/06 9:16 AM

Hide Tools