Saturday, August 29, 2009

Browser Embed In Swing

SWT Browser component is quite handy when you want to include support for platform independent browser support from your swing application.
Its more over important when you want to show modal window with a browser, because in case of non modal window you can anyways start IE instance from Runtime call.

The benefit of using Browser component in Swing is that it gives you enough control over the contents you can change anytime from website.
Also its saves you the time of rewriting the stuff written in Website again in Swing or AWT. Same stuff can be used here by directly pointing to Website Form/Page URL.

You have 2 options to go with.
1. chrriis.dj.nativeswing.swtimpl.components.JWebBrowser and
2. org.eclipse.swt.browser.Browser

I would suggest you to go with SWT Browser component.
One problem you will face with JWebBrowser is that the component creates problem with java 1.6. Its makes whole application
go crazy and all screens seams to get repaint problems.
Another problem you will face in JWebBrowser is that the component shows Custom window then a window.open call is made in
HTML page. You might like to open it in Default webbrowser of your operating system.
It allthemore becomes unacceptable when you have shown JWebBrowser component in JDialog window and you have clicked on window.open link.
It will show a window which is kind of hung and you cannot scroll or do any thing with the new window until you close the modal window.
One more problem you will encounter with JWebBrowser is that if you want to decide if opening of new window is to be allowed should be allowed
or not on the basis of URL location clicked, you have to
make a hidden JWebBrowser Component,
set it as browser instead of new window browser.
Dispose the new window using arg0.getNewWebBrowser().getWebBrowserWindow().dispose() in windowWillOpen open.
Ensure that you dispose the new window before setting the webbrowser.
Cancel the Opening of URL in locationChanging event and get the URL.
These long steps are to be followed if you want to intercept the opening up of new Window.

Good thing about JWebBrowser is that it is licensed under LGPL. So you can use it in your commercial projects also. You need not publish your code to users in case of LGPL.
A word for Christopher Deckers, author of JWebBrowser. Please make it work under Java 1.6. Just a call to NativeInterface.open starts the problems of repainting the windows.

However going with SWT Browser is not a eazy path either. You may face problems like
1. JVM Crashes
2. Not fitting easily in JDialog window and fits in JFrame.
3. Browser opens up in JDialog only once and dont come up next time onwards.
4. Your JDialog window shows up but the Browser dont show up.

To avoid JVM Crashes ensure that your window creation and displaying is all in SwingUtilities.invokeLater.
Let the shell loop be out of SwingUtilities.invokeLater. To ensure that JDialog and Browser both shows up,
you set location, size and visible for canvas, shell and browser component.
To ensure that the browser always comes up on window you might have to make use of small sleeps of 1 second outside your SwingUtilities.invokeLater.
This is to ensure that Window is created and shownup on screen before you try to set the browser component on the window.

The code written using SWT Browser component is completely platform independent. You can run vncserver service on your linux box and connect to it using vncclient and see your
application running in Linux environment using MOZILLA browser instead of IE.


If you need any advice or help on the topic please leave a comment here.

4 comments:

  1. > A word for Christopher Deckers, author of JWebBrowser. Please make it work under Java 1.6. Just a call to NativeInterface.open starts the problems of repainting the windows.

    I stumbled by chance on your blog post :)
    The 0.9.9 preview version available in the SourceForge download section fixes problems with Java 1.6 and adds several new features.
    Java 1.6 update 10 through 14 are problematic releases due to some incompatibilities with some Sun's work in progress, which were resolved in subsequent updates.

    -Christopher

    ReplyDelete
  2. Thanks Christopher for quick update.
    It would be also great if we resolve other issues like
    -new window open from Modal Dialog Box.
    -Giving user access to URL clicked and let the user decide if he wants to continue opening new window or cancel it. Also if user has consumed the event then we can stop creating the new window.

    -Samrat Dhamale

    ReplyDelete
  3. Also a property which once set true, will let user open the new window in Default browser instead of Custom window would be handy for user.

    ReplyDelete
  4. Hi Samrat,

    > new window open from Modal Dialog Box.

    Could you elaborate what the issue is?

    > Giving access to URL clicked and let the user decide if he wants to continue opening new window or cancel it.

    I think this is exactly what the example "JWebBrowser>Nagivation Control" from the demo application is showing.

    > Also a property which once set true, will let user open the new window in Default browser

    But then it would escape the listeners and you would lose control over that new window. Worse, some windows that open are controled by the originating window through Javascript call and opening it externaly would break those pages.
    If it is what you want for known pages, then you simply add a listener to the browser that blocks new windows and calls the JRE Desktop API to open those URLs.

    Hope this helps,
    -Christopher

    ReplyDelete