How to insert Swing widget into SWT I get Exception in thread "main" java.lang.IllegalArgumentException while using SWT_AWT.new_Frame

Mohammed Nishar :

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)
Subash J :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=149154&siteId=1