Reputation: 171
Hi I am new to eclipse and I wanted to add a Swing component like JCombobox to my existing code in SWT. Is there any ways to do it through available API's in SWT or Swing?
I have used SWT_AWT.new_Frame(composite) API which was suggested. Here is my code.
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.NO_BACKGROUND);
Frame myframe = SWT_AWT.new_Frame(composite);
Panel mypanel = new Panel(new BorderLayout()) {
@Override
public void update(java.awt.Graphics g) {
paint(g);
}
};
myframe.add(mypanel);
JRootPane root = new JRootPane();
mypanel.add(root);
java.awt.Container contentPane = root.getContentPane();
String languages[]={"C","C++","C#","Java","PHP"};
final JComboBox cb=new JComboBox(languages);
JScrollPane scrollPane = new JScrollPane(cb);
contentPane.setLayout(new BorderLayout());
contentPane.add(scrollPane);
shell.open();
while(!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
I get below exception.
Exception in thread "main" java.lang.IllegalArgumentException: Argument not valid
at org.eclipse.swt.SWT.error(SWT.java:4533)
at org.eclipse.swt.SWT.error(SWT.java:4467)
at org.eclipse.swt.SWT.error(SWT.java:4438)
at org.eclipse.swt.awt.SWT_AWT.new_Frame(SWT_AWT.java:129)
Upvotes: 2
Views: 477
Reputation: 2048
You have used the proper API actually. But you missed to add the feature like Embedding the AWT widgets into SWT while creating the Composite. SWT.EMBEDDED
Composite composite = new Composite(shell, SWT.NO_BACKGROUND | SWT.EMBEDDED);
Frame frame = SWT_AWT.new_Frame(composite);
Please go through Eclipse help this link for more on this API usage.
Upvotes: 2