SWT Browser - Swing Integration - Mac - JDK 1.7

That's right, so I had an interesting problem related to SWT and swing integration on Mac with java 1.7. I am trying to embed the SWT Browser widget in my swing project as a panel, which is pretty simple for java version 1.6. There were several posts explaining how to do this using the SWT_AWT bridge classes, as well as in the following example:

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MySWTBrowserTest implements ActionListener {

public JButton addCodeButton;
public JButton launchBrowserButton;
public JTextField inputCode;
public JFrame frame;
static Display display;
static boolean exit;

public MySWTBrowserTest() {
    frame = new JFrame("Main Window");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());

    inputCode = new JTextField(15);
    inputCode.setText("999");
    addCodeButton = new JButton("Add Code");
    addCodeButton.addActionListener(this);
    addCodeButton.setActionCommand("addcode");

    launchBrowserButton = new JButton("Launch Browser");
    launchBrowserButton.addActionListener(this);
    launchBrowserButton.setActionCommand("launchbrowser");

    mainPanel.add(inputCode);
    mainPanel.add(addCodeButton);
    mainPanel.add(launchBrowserButton);

    frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("addcode")) {
    } else if (e.getActionCommand().equals("launchbrowser")) {
        createAndShowBrowser();
    }

}

public void createAndShowBrowser() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Canvas canvas = new Canvas();
    f.setSize(850, 650);
    f.getContentPane().add(canvas);
    f.setVisible(true);
    display.asyncExec(new Runnable() {

        @Override
        public void run() {
            Shell shell = SWT_AWT.new_Shell(display, canvas);
            shell.setSize(800, 600);
            Browser browser = new Browser(shell, SWT.NONE);
            browser.setLayoutData(new GridData(GridData.FILL_BOTH));
            browser.setSize(800, 600);
            browser.setUrl("http://www.google.com");
            shell.open();
        }
    });
}

public static void main(String args[]) {
    //SWT_AWT.embeddedFrameClass = "sun.lwawt.macosx.CEmbeddedFrame";
    display = new Display();

    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            MySWTBrowserTest mySWTBrowserTest = new MySWTBrowserTest();
        }
    });

    while (!exit) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}
}

Im uses the JAR files swt-3.8M5- cocoa -macosx-x86_64, which obviously should be included to run the above example. When using the 32-bit and 64-bit versions of 1.6 JDK, this works fine, but when switching to JDK 1.7 or 1.8 VM, a reproducible error occurs:

2012-05-14 15:11:30.534 java[1514:707] Cocoa AWT: Apple AWT Java VM was loaded on first thread -- can't start AWT. (
0   liblwawt.dylib                      0x00000008db728ad0 JNI_OnLoad + 468
1   libjava.dylib                       0x00000001015526f1 Java_java_lang_ClassLoader_00024NativeLibrary_load + 207
2   ???                                 0x00000001015a4f90 0x0 + 4317663120
)
_NSJVMLoadLibrary: NSAddLibrary failed for /libjawt.dylib
JavaVM FATAL: lookup of function JAWT_GetAWT failed. Exit
Java Result: 255

Ive java 1.7 vm , Im , . , : -XstartOnFirstThread VM, SWING/AWT.

, Native Widgets DJ, , SWT.

, JDK 1.7 ( ) mac, : http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops4/S-4.2M7-201205031800/swt-S-4.2M7-201205031800-cocoa-macosx-x86_64.zip, , -XstartOnFirstThread -d64 java 1.7 vm.

, - , Im Im Im , SWT swing 1.7 vm

8 google, , - , Matlab, - .

.


→ UPDATE 1

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

→ 2

: qaru.site/questions/134208/...

+3
1

, . Java 7 AWT CoreAnimation. SWT , AWT Canvas NSView, . - Java 6.

AWT , bugs.sun.com.

+2

All Articles