AWT to SWT Bridge

Hi,

I got a question from a client, he is working on RCP/SWT application, that uses AWT/Swing brdige to SWT.

His problem, is he has a multi tab panels based on swing, and he needs to embed SWT on one of the tabs?

 

Possible ? how ?

Comments

look at the SWT_AWT.new_shell(...) method

On behalf of Natan
hey
some useful links about SWT/Swing issues:
http://www.eclipsezone.com/eclipse/forums/t60541.html
http://dev.eclipse.org/newslists/news.eclipse.platform.swt/msg26445.html

i can't login to the community site
so here's an example for the swt_awt bridge: you can create a sample RCP app with a View(there's such a template in the NewPluginWizard) and replace the code in ViewPart#createPartControl with the following code:

// CREATE SWT OUTER PANEL (WILL CONTAIN THE SWING PANEL)
swtMainPanel = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
final Frame awtFrame = SWT_AWT.new_Frame(swtMainPanel);
awtFrame.setTitle("This is AWT Frame");

// create the SWING panel(will containg the INNER SWT panel)
JPanel swingPanel = new JPanel(new FlowLayout());
awtFrame.add(swingPanel);
// just a Swing button for the example
JButton swingButton = new JButton("SWING Button");
swingPanel.add(swingButton);
swingButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent actionevent) {
JOptionPane.showMessageDialog(awtFrame,
"This is from SWING");

}
});

// create SWING canvas to hold the INNER SWT panel
Canvas awtCanvas = new Canvas();
awtCanvas.setBounds(10, 10, 200, 200);
// add background to distinguish between different widgets
awtCanvas.setBackground(Color.GRAY);
swingPanel.add(awtCanvas);

// embed the new SWT SHELL in the AWT Canvas
Display internalSwtDisplay = Display.getDefault();
final Shell new_Shell = SWT_AWT.new_Shell(internalSwtDisplay , awtCanvas);
// add background to distinguish between different widgets
new_Shell.setBackground(new org.eclipse.swt.graphics.Color(null, 100,0,50));
new_Shell.setSize(100, 100);
new_Shell.setText("A MessageBox Example");

// INNER SWT panel
Composite mainSwtPanel = new Composite(new_Shell, SWT.BORDER);
mainSwtPanel.setSize(80,80);
mainSwtPanel.setBackground(new org.eclipse.swt.graphics.Color(null, 20,60,150));

// just a SWT button for the example
org.eclipse.swt.widgets.Button swtButton = new org.eclipse.swt.widgets.Button(mainSwtPanel, SWT.BORDER);
swtButton.setText("SWT Button");
swtButton.setSize(70,30);
swtButton.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e) {
MessageDialog.openInformation(new_Shell, "SWT Button clicked", "This is from SWT");
}
});

hope it helps
n.

some more links of SWT <=> Swing/AWT conversion

http://wiki.eclipse.org/Albireo_Documentation
http://swik.net/swt/AWT,+Swing+and+Java2D+Integration

i will post another example of Swing->SWT->Swing later

Hi,

  I am trying to call SWT from within AWT and get a thread exception on the highlighted line (final Shell currentShell = SWT_AWT.new_Shell(disp, canvas);)

  Looks like disp always comes up as null. This seems to be a known issue on eclipse.org site. Is there any workaround or fix someone can suggest? Any help is appreciated.

 

Regards,

Sunil







https://bugs.eclipse.org/bugs/show_bug.cgi?id=296681

  

        final Display disp = new Display();

        // I have also tried final Display disp = Display.getCurrentShell() & getDefaultShell();
        final Canvas canvas = new Canvas();
        final Shell currentShell = SWT_AWT.new_Shell(disp, canvas);
        localizationButton.addActionListener( new ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                Display.getDefault().asyncExec( new Runnable()
                {
                    public void run()
                    {
                         LocalizePopupDialog dlg = new LocalizePopupDialog(
                                currentShell, prop, component );

                        if( dlg != null )
                        {
                            dlg.open();
                        }
                    }
                } );
            }
        } );