Enhance your Java application with Java Native Interface (JNI)
How to compete with native applications without sacrificing cross-platform benefits
By Tal Liron, JavaWorld.com, 10/20/99
Page 2 of 7
- Do the native apps have special widgets that your application lacks? Tool tips are common, and so are context menus, but what
about collapsing/expanding, minimizing/maximizing, and showing/hiding? Some GUIs have special popup messages and automatic
behaviors. There's really no limit to what some people think users want.
- Do both types of app handle special mouse and key combinations in the same way? Java makes no special assumptions about the
number of mouse buttons, but many platforms do. Keyboards are largely standardized, but there are exceptions; some keyboards
(and some platforms) have more specialized keys than others. Native applications may exhibit special operating system behavior
for special key combinations.
- Can your application interoperate with the natives? Can you drag and drop between them? What types of objects does each accept?
Dragging files is common, but are other objects commonly dragged in or pasted?
- How does your application interact with the desktop? Some platforms have very cluttered desktops, supporting taskbars, flashing
messages, context menus, and even Web content. Do the native applications use these features? (This article will handle the
taskbar icon feature in Windows, with which you can add application-defined icons to the desktop taskbar that indicate status
without requiring an open window.)
- How does your application interact with the operating system, as compared to the native applications? Do they handle special
OS events, such as hibernation and input methods, in similar ways? Do they converse with system databases or registries? Do
they write to a system event log?
Look at your list of missing features, and don't underestimate its importance. You may not like these features, finding them
weird and difficult to understand, but that's just because you're not used to that platform. The average user knows them,
uses them, and will expect to find them in your application. Unless your application provides functionality that is not available
elsewhere, or is extremely cheap, users will always prefer native options.
But don't go overboard. Some features are so obscure or unpopular that their absence won't be noticed. Filter such features
out, and list only the essentials. Try to think of these as features you want and not just obstacles to overcome. Thinking
positively always helps.
The most important thing is to treat these features as an added benefit (or an annoying redundancy, if you prefer) for users
of a specific platform, but never as features essential to the functioning of the application. Your app should be entirely
and fully usable on either the Standard or Micro edition of the Java platform. This is the only way to protect the application's
cross-platform portability -- and considering that portability was probably why you chose Java as a platform in the first
place, it makes sense to protect it.
Talking the talk
In our exercise above, you probably came up with a list of features your application lacked when compared to native applications
running in the same environment. You may be surprised by how many you can implement purely in standard Java: