Applets Are Applications Too

A technique that I have found to be very useful is to take the time to make sure that JavaApplets can be run as applications too.

This takes some additional work, but in the long run (particularly for large systems), it is definitely worth it. In particular, it confers several benefits that I appreciate:

  1. The system can be unit tested more easily. Stopping and restarting a WebBrowser (all too common) is painful (shades of DOS Alt-Ctl-Del for pointer errors comes to mind).
  2. It can give you more flexibility down the road if you decide to make the system an application.

It does introduce some complexities though. In particular, you will need to maintain 2 versions of the code associated with your applet/application. Additionally, if you are depending upon web server authentication (such as HTTP Basic Authentication), you will need to build some kind of dialog which prompts the user for their login and password.

Finally, if your application requires tight integration with the browser (i.e., lots of JavaScript and other browser-based technologies), then this may not be a good strategy for you.

Thoughts/comments?

-- NicholasJacobs


My few applets are designed to download as quickly as possible. So they don't contain any application code that they don't need to do their applet job.

It should be easy to make a variant of AppletViewer? than can turn any applet into an application by configuring it with the applet's class name. I've never felt the need for this because I can do what I need with AppletViewer? itself. If you want it for a test harness you should develop a test harness. -- DaveHarris


It doesn't take much to make a JavaApplet run as an application. All you have to do is write a main() method that creates a frame and slaps the applet into the frame. In fact, VisualAge for Java automatically generates this kind of code for you when you create an applet. I tend to think that the benefits of writing the few additional lines of code outweigh the (few) additional bytecodes that the method generates.

-- KyleBrown

Well, it takes a little more than that. You need to support getParameter() and a bunch of other stuff. It can (and should) be factored out, so you end up with something like:

    new UK.co.bhresearch.Applet.AppletViewer?( new MyApplet?() );
in the MyApplet? main() method. In which case you might as well also have:
    new AppletViewer?( Class.forName(args[0]).newInstance() );
in the AppletViewer? class. In which case, why bother with the code in MyApplet??

I agree that removing those 100 bytes or so from the applet is an extreme optimization, but in some cases it's justified, given how little extra utility they add. -- DaveHarris

>It doesn't take much to make an Applet run as an application.

You mean a class like Acme.MainFrame? See http://www.acme.com/java/software/Acme.MainFrame.html This class allows applets to stand alone. (However it was written with pre JDK 1.1 event handling.) -- DafyddRees


CategoryJava
EditText of this page (last edited June 13, 2005)
FindPage by browsing or searching

This page mirrored in JavaIdioms as of April 29, 2006