The CDC Toolkit and Xlets
An easy way to describe Xlets is by looking at an example. Once you've installed the CDC toolkit, you'll see an icon for it on the Windows desktop. Running the associated program launches the NetBeans IDE, and this allows you to run any of the examples that are bundled with the CDC toolkit. Figure 6 illustrates one of the simplest examples, called HelloXlet.
Figure 6 Your first view of an Xlet
So, just what is an Xlet? It's an application or service that is run and subsequently controlled by what's called an application manager: the device-resident, vendor-written software that manages the orderly execution of application (or service) software on a mobile device. Using an application manager frees the Xlet programmer from having to worry about many of the details of the mobile environment. The downside is that the Xlet must be written in accordance with a specific class structure.
You can see in Figure 6 that the example Xlet is very simple indeed—just a single message is printed! But if you look at the associated Java code (Listing 1) you'll see that it uses a required application manager pattern.
Listing 1 An Example of an Xlet
/* Copyright c 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ public class HelloXlet implements Xlet { private XletContext context; private Container container; public void initXlet(XletContext context) { this.context = context; try { this.container = context.getContainer(); } catch (UnavailableContainerException e) { e.printStackTrace(); } Component c = new Component() { public void paint(Graphics g) { g.drawString("Hello", 20, 20); } }; c.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.err.println("Mouse clicked"); } }); container.setLayout(new BorderLayout()); container.add(c, BorderLayout.CENTER); } public void startXlet() { container.setVisible(true); } public void pauseXlet() { container.setVisible(false); } public void destroyXlet(boolean unconditional) { } }
Notice the methods in Listing 1: initXlet(), startXlet(), pauseXlet() and destroyXlet(). These methods are called by the application manager in response to user actions; this pattern greatly simplifies the development of Xlet software.
Another interesting example in the CDC toolkit is Java2Ddemo, illustrated in Figure 7.
Figure 7 The Java2Ddemo
The Java2Ddemo example illustrates how to use two-dimensional objects in the mobile environment. You can launch the application in the emulator shown in Figure 7 via the button in the middle of the first row of buttons. As with the Wireless Toolkit example in Figure 3, it's as though you were using a real device. To look at the associated Java code, just open the project files in the Apps folder under the CDCTK10 directory, which is the installation directory for the CDC toolkit.