Click here to Skip to main content
15,895,841 members
Articles / Desktop Programming / Windows Forms

Hosting WPF Content in a Java Application

Rate me:
Please Sign up or sign in to vote.
4.94/5 (8 votes)
26 Apr 2009CPOL4 min read 72.9K   3.1K   28  
Demonstrates a simple technique for embedding WPF/.NET Components into Java GUI
package com.oojni;
import java.awt.Canvas;

/**
 * Wraps the main functionality of Calendar Component.
 * 
 * @author Vitaly Shelest
 *
 */
public class CalendarContainer extends Canvas {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static{
		System.loadLibrary("oojni.net20"); 
		System.loadLibrary("JavaHostWPF");
	}
	/**
	 * Stores a reference to .NET Container
	 */
	int ref = 0;
	protected void finalize() throws Throwable {
		super.finalize();
	}
	/**
	 * The place to create .NET Container
	 */
	public void addNotify() {
		super.addNotify();
		ref = create(this);
	}
	/**
	 * Used to dispose .NET Container
	 */
	public void removeNotify() {
		dispose(ref);
		super.removeNotify();
	}
	/**
	 * Disposes .NET Container
	 *
	 */
	public void dispose()
	{
		if(ref != 0)
			dispose(ref);
		ref = 0;
	}
	/**
	 * Returns Year
	 * 
	 * @return year value
	 */
	int getYearFromControl()
	{
		return getYearFromControl(ref);
	}
	/**
	 * Returns Month
	 * 
	 * @return month value
	 */
	int getMonthFromControl()
	{
		return getMonthFromControl(ref);
	}
	/**
	 * Returns Day
	 * 
	 * @return day of the month value
	 */
	int getDayFromControl()
	{
		return getDayFromControl(ref);
	}
	/**
	 * Sets WPF Clock Component Date/Time
	 * 
	 * @param _year year value
	 * @param _month month value
	 * @param _day day of the month value
	 * @param _hours hour value
	 * @param _minutes minutes value
	 * @param _seconds secons value
	 */
	void setSystemTime(int _year, int _month, int _day, int _hours, int _minutes, int _seconds)
	{
		setSystemTime(ref, _year, _month, _day, _hours, _minutes, _seconds);
	}
	/**
	 * Returns Year
	 * 
	 * @param ref .NET Container reference
	 * @return year value
	 */
	native int getYearFromControl(int ref);
	/**
	 * Returns Month
	 * 
	 * @param ref .NET Container reference
	 * @return month value
	 */
	native int getMonthFromControl(int ref);
	/**
	 * Returns Month
	 * 
	 * @param ref .NET Container reference
	 * @return day of month value
	 */
	native int getDayFromControl(int ref);
	/**
	 * Sets WPF Component Date/Time
	 * 
	 * @param ref .NET Container reference
	 * @param _year year value
	 * @param _month month value
	 * @param _day day of month value
	 * @param _hours hour value
	 * @param _minutes minutes value
	 * @param _seconds seconds value
	 */
	native void setSystemTime(int ref, int _year, int _month, int _day, int _hours, int _minutes, int _seconds);
	/**
	 * Creates .NET Container
	 * @param parent Java component as a placeholder for .NET Container
	 * @return reference to .NET Container
	 */
	native int create(Object parent);
	/**
	 * Disposes .NET Container
	 * @param ref reference to .NET Container
	 */
	native void dispose(int ref);
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Javain Ltd
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions