KJAS Example


One day this will be a real tutorial, but for now here is a minimal example of how to use the applet widget. There's a heavily commented version at the end.

#include <kapp.h>
#include <kwmmapp.h>
#include <kjavaappletwidget.h>
#include <qstring.h>

int main(int argc, char **argv)
{
  KWMModuleApplication app( argc, argv );

  QString a,b,c,d,e,f;

  a = "fred";
  b = "Lake.class";
  c = "file:/dos/pcplus/java/lake/";

  KJavaAppletWidget *applet = new KJavaAppletWidget();
  CHECK_PTR( applet );

  applet->setAppletName( a );
  applet->setAppletClass( b );
  applet->setBaseURL( c );

  applet->create();

  app.connectToKWM();

  d = "image";
  e = "arch.jpg";
  applet->setParameter( d, e );

  applet->show();
  app.exec();
}

Commented version

Here's the code again with (over) heavy commenting to explain each step.

#include <kapp.h>
#include <kwmmapp.h>
#include <kjavaappletwidget.h>
#include <qstring.h>

int main(int argc, char **argv)
{
  // Your application MUST be a KWMModuleApplication, this restriction
  // may be removed in future. It is needed for the addWindow signal
  // used to detect the appearance of the applet window.
  KWMModuleApplication app( argc, argv );

  QString a,b,c,d,e,f;

  // The name of the applet
  a = "fred";
  // The name of the class to be run
  b = "Lake.class";
  // The base url it can be found at. Note the trailing '/', we could
  // also have written it as "file:/dos/pcplus/java/lake/Lake.class"
  c = "file:/dos/pcplus/java/lake/";

  // Create the applet widget. We are using the default
  // context and server implicitly. We could have specified
  // different ones. We could also specify a parent QWidget,
  // but we might as well make the applet widget top level for
  // this example.
  KJavaAppletWidget *applet = new KJavaAppletWidget();
  CHECK_PTR( applet );

  // You must set these BEFORE calling create
  applet->setAppletName( a );
  applet->setAppletClass( b );
  applet->setBaseURL( c );

  // I may make this call unnecessary in future, but for now
  // you must call this to create the Java applet in the JVM.
  applet->create();

  // Attach to KWM so we can see when applet widgets appear
  app.connectToKWM();

  // My test applet needs an image to be specified as a
  // parameter.
  d = "image";
  e = "arch.jpg";
  applet->setParameter( d, e );

  // We show the widget as normal
  applet->show();

  // Then start the event loop
  app.exec();
}