0

I need to call a JNI method that runs a infinite native loop inside, but it stops my java UI.

Java code:

FraMain.java:

public class FraMain extends javax.swing.JFrame {

    public FraMain() {
        initComponents();
        setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);

        System.out.println("here starts");

        new Thread(new Runnable() {

            @Override
            public void run() {
                new JNIEyes().iniciar(new XYListener() {

                    @Override
                    public void onFramePrecesed(double x, double y) {
                        System.out.println(String.format("POINT %f - %f", x,y));
                    }

                });
             }
         }).run();

         System.out.println("continue charging UI");
     }
 }

JNIEyes.java:

public class JNIEyes {

    static {
        System.load("/home/sparkler/NetBeansProjects/OpenEyes/dist/openeyes.so");
    }

    public native void iniciar(XYListener listener);

}

XYListener.java is an interface with the method:

void onFramePrecesed(double x, double y);

and C++ code:

JNIEXPORT void JNICALL Java_jnieyes_JNIEyes_iniciar 
    (JNIEnv *env, jclass clase, jobject listener)
{

    jclass objclass = env->GetObjectClass(listener);
    jmethodID methodID = env->GetMethodID(objclass,"onFramePrecesed", "(DD)V");
    if(methodID == 0){
        printf("\error\n");
        return;
    }

    while (true) {
        Point point = process_image(); 
        env->CallVoidMethod(listener,methodID,point.x,point.y); 
    }

}

The loop itΕ› working fine, and I am getting the x, y values into java onFramePrecesed method, but the UI never shows and the "continue charging UI" message is never printed.

Thanks!

  • 2
    use Thread.start() and not Thread.run() http://stackoverflow.com/questions/8579657/java-whats-the-difference-between-thread-start-and-runnable-run – vandale Jun 18 '14 at 22:20
  • Thanks! you saved me a lot of time! – Pablo Fiorenza Jun 18 '14 at 22:31
  • Make sure you access Swing in a thread safe way. If you are interacting with anything awt/swing related, it needs to be called on the event dispatch thread. See http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html – Alex Barker Jun 18 '14 at 22:39

0 Answers0