Christopher has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

DJ NativeSwing - reloaded: JWebBrowser, JFlashPlayer, JVLCPlayer, JHTMLEditor.

03.12.2008
| 45471 views |
  • submit to reddit

Today's release of DJ Native Swing 0.9.4 greatly improves stability and brings new components: in addition to the JWebBrowser and JFlashPlayer, there is now the JVLCPlayer and the JHTMLEditor.
Here is a summary of what to expect when using this library.

 

1. Various native components

DJ Native Swing was designed to handle all the complexity of native integration, mainly in the form of components with a simple Swing-like API.

Here are some screenshots of several components in action (click to enlarge):

JWebBrowser:
JFlashPlayer:
JWebBrowser JFlashPlayer
JVLCPlayer:
JHTMLEditor:
JVLCPlayer JHTMLEditor

 

2. Simple API

First, we need to initialize the framework. This needs to happen before any feature is used. A common place for this call is the first line of the main():

public static void main(String[] args) {
NativeInterfaceHandler.init();
// Here goes the rest of the initialization.
}

Now, let's see how to create a JWebBrowser, with its URL set to Google's homepage:

JWebBrowser webBrowser = new JWebBrowser();
webBrowser.setURL("http://www.google.com");
myContentPane.add(webBrowser);

Note that we set a URL, but we could as well set the HTML text. The JWebBrowser also allows to execute Javascript calls, and we can even propagate notifications from custom pages to our Swing application.

Moving to a more practical example, we may want to be notified of URL change events or track window opening events, potentially preventing navigation to occur or open the page elsewhere. This is easily achieved by attaching a listener:

webBrowser.addWebBrowserListener(new WebBrowserAdapter() {
public void urlChanging(WebBrowserNavigationEvent e) {
String newURL = e.getNewURL();
if(newURL.startsWith("http://www.microsoft.com/")) {
// Prevent the navigation to happen.
e.consume();
} else {
// We can consume the event and decide to open this page in a tab.
}
}
public void windowWillOpen(WebBrowserWindowWillOpenEvent e) {
// We can prevent, add the URL to a tab, etc.
}
// There are of course more events that can be received.
});

Let's have a look at the JFlashPlayer:

JFlashPlayer flashPlayer = new JFlashPlayer();
flashPlayer.setURL(myFlashURL);

The JFlashPlayer can open local or remote Flash files, and files from the classpath; the latter being a general capability of the library by proxying files using a minimalistic web server.
Of course, the JFlashPlayer allows to retrieve and set Flash variables, and play/pause/stop the execution.

The JVLCPlayer and the JHTMLEditor are no exceptions to this simplicity: playlist can be manipulated in the VLC player, HTML can be set and retrieved from the HTML editor, etc.

There is also the possibility to integrate Ole controls on Windows, still with a simple Swing-like API. An example is provided in the library in the form of an embedded Windows Media Player.

References
Published at DZone with permission of its author, Christopher Deckers. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Christopher Deckers replied on Fri, 2008/08/22 - 10:06am in response to: puneet dixit

Hi,

The Flash plugin may be installed but not on XULRunner. To fix that, you may have to run a command like:
sudo ln -s -t /usr/lib/xulrunner/libflashplayer.so /usr/lib/mozilla/plugins/libflashplayer.so

Hope this helps!
-Christopher

ramesh kancherla replied on Wed, 2008/08/27 - 1:11am in response to: Christopher Deckers

Hi Christopher,

                    This is Ramesh Kancherla. I have implemented DJ WebBrowser in my  frame. 

Is there any method for disabling calander popup window maximize button.

can you please give me suggestion.

 

Thanks & Regards

Ramesh K

Christopher Deckers replied on Wed, 2008/08/27 - 1:31am in response to: ramesh kancherla

Hi Ramesh,

NativeSwing does not have any such specific API I think. I have some suggestions though:
- If your popup is in a separate window, then you may attach a browser listener to track when it gets open, then on the JWebBrowserWindow (which you can obtained with webBrowser.getWebBrowserWindow()) you could setResizable(false).
- If the popup is a Javascript layer, then you may try to check if some Javascript could do what you want.

-Christopher

 

Lucas de la Fuente replied on Mon, 2008/11/17 - 7:04am

Hi Christopher, I'm having problems with the flash(as3)-Java communication. The ExternalInterface.call method in ActionScript is not getting response from Java. I see you have embedded some variables in the flash, but how can I communicate directly from as3 code to the commandReceived method ? All the other parts are working perfectly.

 

   Anyway, thanks, great job.

Christopher Deckers replied on Mon, 2008/11/17 - 7:46am in response to: Lucas de la Fuente

Hi,

> The ExternalInterface.call method in ActionScript is not getting response from Java

Are you calling the JS sendCommand(args) using the Flash ExternalInterface? Did you have a look at the demo application, more specifically the Function Calls example?

Don't hesitate to contact me by e-mail (chrriis (at) nextencia (dot) net) to give me a bit more details, so that I can try to help you.

-Christopher

 

Lucas de la Fuente replied on Mon, 2008/11/17 - 8:50am in response to: Christopher Deckers

Yes, I'm looking that sample again and again, but the problem is in the .as, I think.

 

  Im calling ExternalInterface.call("thisCommand","thisArgOnly");

  Then, in Java:

 

(...)

flashPlayer.addFlashPlayerListener(new FlashPlayerListener() {
      public void commandReceived(String command, String[] args) { 

 //Here the string comparation: if("thisCommand".equals(command)....etc

 ________________

Now I'm wondering if I'm talking only to the embedded browser and I need some other JavaScript code to make this work...

 

Christopher Deckers replied on Mon, 2008/11/17 - 9:15am in response to: Lucas de la Fuente

> Im calling ExternalInterface.call("thisCommand","thisArgOnly");

I think there is an error in that line. It should be:

ExternalInterface.call("sendCommand", "thisCommand","thisArgOnly");

What this does is invoke a magic JS sendComman() function, which will internally call the FlashListener with your arguments ("thisCommand" and "thisArgOnly").

Please let me know if that was the issue.

-Christopher

 

Lucas de la Fuente replied on Mon, 2008/11/17 - 9:30am in response to: Christopher Deckers

Yes, that was the problem...Sorry for my blindness. Now all things work together.

 

 Many thanks !

 

PS: I had difficult to reach that JS information in the samples, I don't know if it's easily available, but one mention in a more visible place ( like here now ;) ) will be an unvaluable help for others.

Again, many thanks for this project.

Lucas de la Fuente replied on Mon, 2008/11/17 - 9:40am in response to: Lucas de la Fuente

OK, It was in the most visible place of the sample....:s, I had eyes only for the code....But I still think it will be more easy if it says: "ExternalInterface.call("sendCommand","yourMethodName",arg1,arg2,...)

It's not clear here "sendCommand" is mandatory, and arg1 the method name etc.

 

 

Melih Öztürk replied on Sun, 2008/12/07 - 11:06am

Hi Christopher,

First of all thank you for bringing us this amazing tool, which is way better than its alikes. I am actually interested in importing some tools to DJ Native Swing and i decide to start with importing Firebug into this browser when i see the XPCOM support. So what should i do to import Firebug to DJ Native Swing web browser? I checked out some XUL samples in Mozilla project website but they are not adequate for my purpose. 

Thanks again.

Christopher Deckers replied on Sun, 2008/12/07 - 12:07pm in response to: Melih Öztürk

Hi,

I don't know what is the best approach. Since FireBug is a Mozilla extension, you may be able to use it as an extension to XULRunner. Once installed, maybe some XPCOM APIs would allow you to activate it, but this is an area I don't know anything about.

In case you want to continue this discussion, feel free to e-mail me (chrriis at nextencia dot net).

-Christopher

 

suresh kumar replied on Sat, 2009/02/14 - 4:56am

Dear Chris, I use DJ Native Swing 0.9.5 for running flash player in LINUX.Its works very fine. But, sometimes the flash movie is not loading and blank white screen is displayed. This problem is getting sorted only after restarting the LINUX PC.(Note: We are setting the MOZILLA variable and path during LINUX startup). Will the new version 0.9.7 solve this issue? I am using 0.9.5. Any Suggestions?

Jan Gurda replied on Thu, 2009/02/26 - 10:29am

Hi

 

 

I have one question. I am using JWebBrowser in the JDialog, and every time I create this dialog I am also creating JWebBrowser component. Problem is  that memory of whole java process is rising. I dispose JWebBrowser during closing dialog, but memory level is not going down. After invoking 30 dialog creations memory rises about 60 Mb. Can anybody help me and give a clue what is wrong?

 

Thank you.

 

Regards,

 

John

javier parra replied on Wed, 2009/07/22 - 12:42pm

Hi Christopher i just download the project and i am trying to make an aplication (web browser) but i have some problems one of them (hahahaha there are a lot of them) where can i download the .jar for interfacehandler ? or i have to make my own class for that.  well thats all for now i hope you can help me thanx in advance

bye

Christopher Deckers replied on Wed, 2009/07/22 - 1:36pm in response to: javier parra

Hi,

I am not sure what you mean by the JAR with interfacehandler: there is no such class in the project. Nevertheless, all that is needed are the NativeSwing JARs and the JARs from the libs folder (not all are needed, it depends from the feature you use, but at leat the JNA JAR files and the SWT JAR file are required).

If you need more help, do not hesitate to post your question on the help forum: https://sourceforge.net/forum/forum.php?forum_id=671154

-Christopher

hector tino replied on Tue, 2009/09/15 - 5:29am

Hi Christopher, I used JWebBrowser, Its working fine but i need add Keylistner?.I want to exit the JWebBrowser when i pressed the ESC Key. for this i used this code:
webBrowser.getNativeComponent().addKeyListener(new java.awt.event.KeyAdapter() {
                    public void KeyPressed(java.awt.event.KeyEvent evt) {
                        int keyCode = evt.getKeyCode();
                        switch (keyCode) {
                            case KeyEvent.VK_BACK_SPACE:
                            case KeyEvent.VK_WINDOWS:
                            case KeyEvent.VK_CONTROL:
                            case KeyEvent.VK_ESCAPE:
                                muestraVisualizator();//Method for exit
                                vanhack.requestFocus();
                                break;
                        }
                    }
                    });
but this don´t work :S how can i get a keylistener from your jWebBrowser? Thank you very much for your help, and sorry for my bad english :S Vanhack

Christopher Deckers replied on Tue, 2009/09/15 - 1:17pm in response to: hector tino

Hi,

I think I found a mistake in your code which makes a difference. Instead of:
public void KeyPressed(java.awt.event.KeyEvent evt)
put:
public void keyPressed(java.awt.event.KeyEvent evt)

And with that change, it works for me.

For future questions, I would recommend using the Help forum here.

Hope this helps,
-Christopher

Jagadis Natarajan replied on Thu, 2010/04/08 - 3:12am

Hi Chris,

Absolutely Love what you have done :).... It Solved me weekends of head aches ;).... Is it possible to use only Mozilla as default instead of IE? i want to do this beacuse when i load some sites which have a lot of flash content the application hangs for at elast 10 minutes before resuming operation.

 

 

Thanks a lot for this wonderful tool :)

 

Regards,

Jagadis

Christopher Deckers replied on Thu, 2010/04/08 - 6:11am in response to: Jagadis Natarajan

Hi Jagadis,

> Is it possible to use only Mozilla as default instead of IE?

 Yes, provided you install or ship XULRunner (the Mozilla runtime). You need to construct the web browser using a special option. Refer to the demo application for usage.

If you have any other questions, please go to the DJ Project forum.

-Christopher

Jagadis Natarajan replied on Sun, 2010/04/11 - 10:41am

Thanks Chris for the prompt reply..... Will check it out... Sure to get doubts..... Will post here if i have them :)

 

Thanks again.... highly appreciate your work!!!!! Rock on \m/ :) 

Jagadis Natarajan replied on Sun, 2010/04/11 - 7:01pm

One more question,

The Application seems to hang on for about 10 minutes whenever a site with lots of flash content loads (Unable to close, unable to navigate away from the site.. have to sit for 10 mins) and thn it responds. 

 

Is this a known issue or is it just occuring to me.... if its just me then i'll take a better look at my code 

 

Regards,

Jagadis

Christopher Deckers replied on Mon, 2010/04/12 - 5:25am in response to: Jagadis Natarajan

Hi Jagadis,

I have never heard of such issue. If you have an actual web site I could test please let me know.

But please, do not comment here. Add a post in the forum. That way, other users may be able to help and your problem would help other users.

-Christopher

Umme Habiba replied on Wed, 2011/07/13 - 12:40am

Hi Chris,

I used JWebBrowser, Its working fine but I want to handle JWebBrowser's  "navigate(uri)" method with its return true or false value. But its always returning false whether the page load succesfully or not.

        final JWebBrowser webBrowser = new JWebBrowser();
        boolean canDisplay = webBrowser.navigate(uri);

        if(!canDisplay){

//             load a new default page.

         }

Ican't make it working. Can you please help with this problem.

I'm using windowsXP, and java swing here.

Christopher Deckers replied on Wed, 2011/07/13 - 6:11am in response to: Umme Habiba

Hi Umme,

The result indicate whether the browser accepted the request. Not whether the page was succefully loaded. In any case, detecting whether a page loaded probably does not mean much because of possible HTTP redirects, dedicated 404 pages and so on.

The closest you can do is add a web browser listener and respond to some events (locationChangeCanceled).

By the way, next time (or if you want to respond), create a topic in the help forum.

-Christopher

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.