RE: [java-gnome-hackers] Looking forward
Brought to you by:
afcowie
From: Jeffrey M. <Jef...@Br...> - 2004-01-20 15:26:54
|
> > Luca, if you are still interested in learning some of the JNI > > code I could work with you on the FileChooser. > > > That would be great! I was just thinking to take a look at FileChooser > (at least to know how it looks;) > Remember, however, that I'm with that old pc, and pratically it's > impossible for me to compile. I could always do with a simple text > editor, but without the possibility to test further changes... You can do some of the editing and I'll do the testing 8-) I just added a class FileFilter that needs some native code. You can start by compiling this file and then running jdk utility to create the initial JNI header file like: javah org.gnu.gtk.FileFilter This will create a file called org_gnu_gtk_FileFilter.h in the directory where you run it. Copy this file to gtk/src/jni/org_gnu_gtk_FileFilter.c (notice I changed the extension from .h to .c). Now the real fun begins!! The first thing we need to do is update the header and then convert all of the method decls to method impls. For the header you can just copy/paste it from another .c file in the package. This will make sure the file "includes" the correct headers. For the methods you can follow the following examples: Example 1: .h JNIEXPORT jint JNICALL Java_org_gnu_gtk_FileFilter_gtk_1file_1filter_1new (JNIEnv *, jclass); .c JNIEXPORT jint JNICALL Java_org_gnu_gtk_FileFilter_gtk_1file_1filter_1new (JNIEnv *env, jclass cls) { return (jint)gtk_file_filter_new(); } Example 2: .h JNIEXPORT void JNICALL Java_org_gnu_gtk_FileFilter_gtk_1file_1filter_1set_1name (JNIEnv *, jclass, jint, jstring); .c JNIEXPORT void JNICALL Java_org_gnu_gtk_FileFilter_gtk_1file_1filter_1set_1name (JNIEnv *env, jclass cls, jint filter, jstring name) { gchar* n = (gchar*)(*env)->GetStringUTFChars(env, name, NULL); gtk_file_filter_set_name((GtkFileFilter*)filter, n); (*env)->ReleaseStringUTFChars(env, name, n); } In the first example we just make a call to a native gtk method and cast the results to a jint during the return. In the second example you see that I am calling a JNI method to read the string from the jstring, calling the native method, and then freeing the memory used by the gchar*. The filter parameter is nothing more than a jint that holds the address of the underlying native struct. Most methods should be this easy but occasionally you will find a method that will require more code to convert data types, etc. Please give this a try and let me know if you get stuck on anything. -Jeff |