Click here to Skip to main content
15,892,809 members
Articles / Containers / Virtual Machine

Embedding a JavaBean in a MFC-Dialog

Rate me:
Please Sign up or sign in to vote.
4.56/5 (15 votes)
5 Mar 20044 min read 49.3K   814   32  
Embedding a JavaBean without using Sun's ActiveX-Bridge.
// ore.cpp: Implementierung der Klasse Core.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "stdafx.h"
#include "ore.h"
#include <assert.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif




//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
namespace JBridge
{

jclass ClassCache::jframe = 0;
jclass ClassCache::jembeddedframe;

jmethodID MethodCache::jframe_getContentPane = 0;
jmethodID MethodCache::jframe_constr = 0;
jmethodID MethodCache::jframe_show = 0;
jmethodID MethodCache::jframe_setBounds = 0;
jmethodID MethodCache::jframe_setVisible = 0;
jmethodID MethodCache::jframe_add = 0;
jmethodID MethodCache::jframe_dispose = 0;



AttachCurrentThread::AttachCurrentThread(bool autodetach) : detach(autodetach)
{
	jint res = JVMLoader::getInstance()->getJVM()->AttachCurrentThread((void**)&env, NULL);

	assert(res >= 0);
}
AttachCurrentThread::~AttachCurrentThread()
{
	if (detach)
		JVMLoader::getInstance()->getJVM()->DetachCurrentThread();
}

GetStringUTFChars::GetStringUTFChars(jstring str, JNIEnv *e) : jStr(str), env(e)
{
	assert(env != NULL);
	if (str != NULL)
		cStr = env->GetStringUTFChars(jStr, NULL);
	else
		cStr = NULL;
}

GetStringUTFChars::~GetStringUTFChars()
{
	env->ReleaseStringUTFChars(jStr, cStr);
}



JFrame::JFrame(const char *title) : NewObject(true)
{
	AttachCurrentThread thread;

	object = thread.getJNIEnv()->NewObject(ClassCache::jframe, MethodCache::jframe_constr, thread.getJNIEnv()->NewStringUTF(title));

}
JFrame::JFrame(jobject jframe) : NewObject(false)
{
	object = jframe;
}

jobject JFrame::getContentPane()
{
	AttachCurrentThread thread;

	return thread.getJNIEnv()->CallObjectMethod(object, MethodCache::jframe_getContentPane);
}
void JFrame::show()
{
	AttachCurrentThread thread;

	thread.getJNIEnv()->CallVoidMethod(object, MethodCache::jframe_show);
}
void JFrame::dispose()
{
	AttachCurrentThread thread;

	thread.getJNIEnv()->CallVoidMethod(object, MethodCache::jframe_dispose);
}

void JFrame::setBounds(int x, int y, int width, int height)
{
	AttachCurrentThread thread;

	thread.getJNIEnv()->CallVoidMethod(object, MethodCache::jframe_setBounds, x, y, width, height);
}
void JFrame::setVisible(jboolean visible)
{
	AttachCurrentThread thread;
	thread.getJNIEnv()->CallVoidMethod(object, MethodCache::jframe_setVisible, visible);
}
jobject JFrame::add(jobject component)
{
	AttachCurrentThread thread;

	return thread.getJNIEnv()->CallObjectMethod(getContentPane(), MethodCache::jframe_add, component);
}

jobject JFrame::operator*()
{
	if (NewObject)
	{
		AttachCurrentThread thread;
		return thread.getJNIEnv()->NewLocalRef(object);
	}
	else
	{
		return object;
	}
}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Chief Technology Officer W3L
Germany Germany
-Since 1th August 2007: Chief Technology Officer of W3L
-2002/08/01-2007/07/31: PhD student
-1997/10/15-2002/07/31: Studied Electrical Engineering and Computer Science

Comments and Discussions