Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Writing Java for the Palm

Java 2 Micro Edition and its K Virtual Machine (KVM) allow you to write Java applications for handheld devices, including ones that run Palm Computing's Palm OS. Learn to write a Hello World Spotlet for the Palm using KVM.


advertisement
ava continues to make inroads at all levels of computing. Most recently, Java has returned to its roots and found a home in the small devices for which it was originally designed. Java 2 Micro Edition and its K Virtual Machine (KVM) allow you to write Java applications for handheld devices, including ones that run Palm Computing's Palm OS. You can download an early version of the KVM from http://java.sun.com/products/kvm.

The KVM includes a reduced set of APIs from the Java 2 platform's java.io, java.lang, java.net, and java.util packages. It also includes a package called com.sun.kjava which includes user interface and event handling classes for writing applications. At the moment, it only appears to support the Palm OS.

You can write a Palm application by subclassing com.sun.kjava.Spotlet, which provides callbacks for handling events. The KVM manages the event loop and forwards events to the Spotlet, which are handled by methods such as keyDown() and penDown(). A Spotlet first needs to register its event handlers with the register() method before it is able to receive events. This is very different from the traditional AWT/Swing event model. In fact, none of your AWT or Swing code will port to the current incarnation of the KVM. You unregister your event handlers with unregister().



You can compile your KVM apps for the Palm using your regular Java compiler, setting the classpath to use the KVM classes. But you'll need to use the utilities included with the KVM distribution in order to convert your programs into a format you can download to your Palm. These are the palm.database.MakePalmApp program and the palm.database.MakePalmDB program. MakePalmApp will convert a Java program into a Palm .prc file that will automatically load the KVM to run the program when invoked on the Palm. MakePalmDB allows you to add classes to the KVM's class database so that multiple KVM applications may share classes.

The code below demonstrates a simple HelloWorld Spotlet which displays some text and an exit button:

/*** * To compile adjust the bootclasspath to point to the KVM classes. * javac -bootclasspath kvmDR4.1_bin/api/classes.zip HelloWorld.java * To make a .prc file add the KVM tools to your classpath. * java -classpath kvmDR4.1_bin/tools/classes.zip \ * palm.database.MakePalmApp HelloWorld ***/ import com.sun.kjava.*; public class HelloWorld extends Spotlet { private Button __exitButton; static final String _HELLO_WORLD = "Hello World!"; static final Graphics _GRAPHICS = Graphics.getGraphics(); public HelloWorld() { __exitButton = new Button("Exit", 16, 144); _GRAPHICS.clearScreen(); paint(); } public void paint() { __exitButton.paint(); _GRAPHICS.drawString(_HELLO_WORLD, 48, 72); } public static void main(String[] args) { (new HelloWorld()).register(NO_EVENT_OPTIONS); } public void penDown(int x, int y) { if(__exitButton.pressed(x, y)) System.exit(0); } }



   
Daniel F. Savarese holds a B.S. in astronomy and an M.S. in computer science, both from the University of Maryland, College Park. He is the author of the OROMatcher regular expression library for Java. Reach him here.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date