The book of inspiration

October 21, 2009

Extending ScriptCoreLib

Filed under: jsc — zproxy @ 5:25 pm

In this post I am going to show you how to implement yet another .net class for java. It is going to be Windows.Forms.Treeview.

This will be included in the next jsc release.



It is actually a regular .net windows forms application (see c# source code)!


Now that we know what we are going to be implementing lets check first that it does not already exist. The project we need to open is ScriptCoreLibJava.Windows.Forms.


I can see TextBox but I cannot see TreeView. Let’s continue and add it then. The very first thing we do is to define an internal class which is marked to be implementing the TreeView class. At this time we do not define any members to it yet.


Now it is time to create a test project.


Before we can use this new project we should do a debug build. Some referenced assemblies will be rebuilt in doing this.


We are only interested in java and thus we proceed to disable javascript generation. This is done by commenting out a line in the post build event.


We should now check if the project template can be translated to java by launching a release build.


The first build will take some time. Go check your emails:)

You should now have a java version of your application. There are actually two bat files. Both of them will launch the java version of the application. The fusion version will use the jar file which have been embedded in the .net exe file.



At this point I chose to this project into the svn.

To continue let’s switch back to debug build and add a treeview to the Form1 with some nodes.


This was all done in the designer. To see which API we must now go and actually implement to make it work for java we need to open the generated code.


As we can see we will also need to add TreeNode and TreeNodeCollection.


Now it is time to investigate the generated InitializeComponent code to see which API we must implement. It seems we will need some constructors and properties.


After this step we will switch to release mode and launch a build to catch any missing API calls. It seems we got zero errors. This means we can continue with implementation. For that we need to use JTree. We need to add a native stub class as it does not yet exist.



Adding stubs is still a semi manual job. I have set up a web service which will generate C# source code to serve as stubs for ScriptCoreLib. There are a lot of native stubs to be added and the service currently does not support nested types within interfaces, which is not allowed in c# mind you. There is a trick to allow nested stubs in interfaces but currently I will comment out the classes that do not fit the current support.


Now that we actually have the native stubs defined our next job is to make that JTree appear where we would want our TreeView to appear. We will omit nodes at first.


Let’s switch to release build mode and try to build this application for java runtime.


If all works we will have a white box where our nodes should be.

But we are not that lucky.


Aha! We forgot to initialize the Nodes property.


After next rebuild we get a nice surprise where instead of a white box there are some example nodes. Whats up with that? 🙂


After reading some documentation it became clear that I would need to create a hidden root node.


Basically I got it working. Now I need to improve the Clear method and add event support.

A few hours later and it is looking really nice 🙂



Create a free website or blog at

%d bloggers like this: