Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
After reading this article you will be able to:
Consider a simple C program with a Motif user interface. By browsing through its menus, currently the user can create OpenGL
windows that contain some simple 3D rendering. You have been asked to add a new menu to this application; you want to implement
it using the Swing package from the JDK. The Java Native Interface (JNI) API from the Java Runtime Environment provides you
with the tools you need to start a JVM from a C program and accomplish the task at hand. Using that JVM, JNI also allows you
to create Java objects and call the objects' methods. Assuming that you have written the code needed for a Java class that
implements the required menu (see SwingMenu.java for the full Java code, available for download from Resources), you now have to access the JNI from the C code to start the JVM and load your class. Listing 1 illustrates those last two
tasks:
Listing 1: Start the JVM and load a class
/** File main.c **/
#include <stdlib.h>
#include <jni.h>
int main (int argc, char *argv[])
{
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
int nbOptions;
jint res;
jclass myJavaClass;
jobject javaGUI;
jmethodID constructorID;
char *classPath, *libraryPath;
/** Code to be added here **/
/** Start the Java Virtual Machine **/
classPath = malloc(29);
strcpy(classPath, "-Djava.class.path=/myClassDir"); /* Path of the
SwingManu.class file */
libraryPath = malloc(29);
strcpy(libraryPath, "-Djava.library.path=/myLibDir"); /* Path of
the libNativeMethods.so library */
nbOptions=0;
options[0].optionString = classPath; nbOptions++;
options[1].optionString = libraryPath; nbOptions++;
vm_args.version = JNI_VERSION_1_2; /* Specifies the JNI
version used */
vm_args.options = options;
vm_args.nOptions = nbOptions;
vm_args.ignoreUnrecognized = JNI_TRUE; /* JNI won't
complain about unrecognized options */
res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);
free(classPath);
free(libraryPath);
/*
* Create an instance of the Class SwingMenu.
* Before calling NewObject we must get the constructor
* of the Java class. This constructor must de passed
* as an argument to NewObject.
*/
myJavaClass = (*env)->FindClass(env, "SwingMenu");
constructorID = (*env)->GetMethodID(env,myJavaClass,"<init>",
"()V");
javaGUI = (*env)->NewObject(env,myJavaClass,constructorID);
}
Job done! You are now able to start your application. With a few Java method invocations through JNI you can see your Swing menu on the screen. Below is the picture of your application at startup.
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq