简体   繁体   中英

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM