Updates from February, 2007 Toggle Comment Threads | Keyboard Shortcuts

  • tech0x20 10:23 pm on February 16, 2007 Permalink | Reply  

    Java: Preventing a child window’s close operation from exiting the app. 

    I included a second JFrame in NetBeans, which I used as a child window. When the child window was closed, all windows were closed. I finally figured out that the default close operation needed to be either HIDE_ON_CLOSE or DISPOSE_ON_CLOSE. The default in Java is HIDE_ON_CLOSE, but in NetBeans is EXIT_ON_CLOSE.

    I first changed this property in the main window code that invokes the child window, but then I looked at the Properties page for the child window. “defaultCloseOperation” is the first property. “DISPOSE” can be selected here.

    From Sun Java Documentation

    setDefaultCloseOperation

    public void setDefaultCloseOperation(int operation)
    Sets the operation that will happen by default when the user initiates a “close” on this frame. You must specify one of the following choices:

    • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don’t do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
    • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
    • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
    • EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

    The value is set to HIDE_ON_CLOSE by default.

     
  • tech0x20 10:13 pm on February 14, 2007 Permalink | Reply  

    Link to Random Image Password Generation 

    Captchas.net

     
  • tech0x20 6:26 pm on February 10, 2007 Permalink | Reply  

    Reducing redundant code by wrapping JNI functions. 

    My HelloWorldNative.c, with the “nativePrintObject” function reduced by wrapping the object and integer field retrieval:


    JNIEXPORT void JNICALL Java_helloworld_Main_nativePrintObject(JNIEnv *env, jobject ths, jobject wsv)
    {
    jint ipAddress;
    jstring jPageVisited;
    char *pageVisited;
    jstring jRefererURL;
    char *refererURL;
    jobject ipObject;

    ipAddress = getIntegerField(env, wsv, "ipAddress");
    printf(" wsv.ipAddress = %d.%d.%d.%dn",
    ( ipAddress & 0xff000000 ) >> 24,
    ( ipAddress & 0x00ff0000 ) >> 16,
    ( ipAddress & 0x0000ff00 ) >> 8,
    ipAddress & 0x000000ff);

    if(!(jPageVisited = getStringField(env, wsv, "pageVisited"))) {
    return;
    }

    pageVisited = (*env)->GetStringUTFChars(env, jPageVisited, 0);

    if(!pageVisited) {
    return;
    }
    printf(" wsv.pageVisted = %sn", pageVisited);
    (*env)->ReleaseStringUTFChars(env, jPageVisited, pageVisited);

    if(!(jRefererURL = getStringField(env, wsv, "refererURL"))) {
    return;
    }

    if(!(refererURL = (*env)->GetStringUTFChars(env, jRefererURL, 0))) {
    return;
    }
    printf(" wsv.refererURL = %sn", refererURL);
    (*env)->ReleaseStringUTFChars(env, jRefererURL, refererURL);

    if(!(ipObject = getObjectField(env, wsv, "ipAddr", "helloworld", "IPAddress"))) {
    return;
    }

    printf(" wsv.ipAddr = %d.%d.%d.%dn",
    getIntegerField(env, ipObject, "byte1"),
    getIntegerField(env, ipObject, "byte2"),
    getIntegerField(env, ipObject, "byte3"),
    getIntegerField(env, ipObject, "byte4")
    );
    }

    jniUtils.h, the header for the JNI wrapper:


    /*
    * File: jniUtils.h
    * Author: thomas
    *
    * Created on February 10, 2007, 5:30 PM
    */
    #include <jni.h>

    #ifndef _jniUtils_H
    #define _jniUtils_H

    #ifdef __cplusplus
    extern "C" {
    #endif
    jint getIntegerField(JNIEnv *env, jobject ths, char *fieldName);
    void putIntegerField(JNIEnv *env, jobject ths, char *fieldName, jint ji);
    jstring getStringField(JNIEnv *env, jobject ths, char *fieldName);
    jobject getObjectField(JNIEnv *env, jobject ths, char *fieldName,
    char *objectPackage, char *objectClass);

    #ifdef __cplusplus
    }
    #endif


    #endif /* _jniUtils_H */

    jniUtils.c, the wrapper for the JNI C interface. Note that not all types have been implemented. Also, error checking may not be performed in all scenarios. See that the “signature” for objects is broken up into package and class name.


    #include <stdlib.h>
    #include <string.h>
    #include "jniUtils.h"

    jint getIntegerField(JNIEnv *env, jobject ths, char *fieldName)
    {
    jclass c;
    jfieldID fid;

    c = (*env)->GetObjectClass(env, ths);
    fid = (*env)->GetFieldID(env, c, fieldName, "I");
    return (*env)->GetIntField(env, ths, fid);
    }

    void putIntegerField(JNIEnv *env, jobject ths, char *fieldName, jint ji)
    {
    jclass c;
    jfieldID fid;

    c = (*env)->GetObjectClass(env, ths);
    fid = (*env)->GetFieldID(env, c, fieldName, "I");
    (*env)->SetIntField(env, ths, fid, ji);
    }

    jstring getStringField(JNIEnv *env, jobject ths, char *fieldName)
    {
    jclass c;
    jfieldID fid;

    return (jstring) getObjectField(env, ths, fieldName, "java/lang", "String");
    }

    jobject getObjectField(JNIEnv *env, jobject ths, char *fieldName,
    char *objectPackage, char *objectClass)
    {
    char *objectSignature;
    jclass c;
    jfieldID fid;
    jobject objectField = 0;

    objectSignature = (char *) malloc(strlen(objectPackage) + strlen(objectClass) + 10);

    strcpy(objectSignature, "L");
    strcat(objectSignature, objectPackage);
    strcat(objectSignature, "/");
    strcat(objectSignature, objectClass);
    strcat(objectSignature, ";");
    c = (*env)->GetObjectClass(env, ths);
    fid = (*env)->GetFieldID(env, c, fieldName, objectSignature);

    if(!fid) {
    return 0;
    }
    objectField = (*env)->GetObjectField(env, ths, fid);

    free(objectSignature);

    return objectField;
    }
     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
%d bloggers like this: