Protostome
Protostome

Reputation: 6039

Adding Swing components to Eclipse RCP plugin

I'm having trouble embedding Swing components inside SWT (such as eclipse plugin..) Currently what I have:

 public void createPartControl(Composite parent) {
  java.awt.Frame f = SWT_AWT.new_Frame(parent);
  JPanel panel = new JPanel(new BorderLayout());
  JButton button = new JButton("Swing button");
  JLabel label = new JLabel("Swing label");
  panel.add(label,BorderLayout.NORTH);
  panel.add(button,BorderLayout.CENTER);
  f.add(panel);
 }

This code snippet fails to load, the plugin crashes on the first line...

Any idea how to incorporate these components?

Thanks!

Upvotes: 3

Views: 7855

Answers (2)

Favonius
Favonius

Reputation: 13984

Since your code is failing at the first line then please first make sure that the parent Composite is created using SWT.EMBEDDED. If it is not then create a child composite using the SWT.EMBEDDED and then call

java.awt.Frame f = SWT_AWT.new_Frame(newChildComposite);

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.

Taken from Article-Swing-SWT-Integration

Upvotes: 2

Will
Will

Reputation: 6286

http://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html

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);

Upvotes: 5

Related Questions