How to build Java application with WinForms interface
Javonet Quick Start Guide
For more information how to use Javonet in common scenerios like creating objects, invoking methods, getting/setting fields, subscribing events and others please refer to our Javonet Quick Start Guide
Overview
This sample show how to build .NET WinForms window from Java code. If you look for a solution how to embed existing .NET WinForms or WPF user control within Java AWT, Swing or JavaFX window check our guide here: Embedding .NET User Controls in Java
Download Sample Project
Use the links below to download full source code and binaries of the sample project created in this tutorial:
Download Source Code and Binaries
Introduction
In this tutorial you will learn how to build Java application with WinForms interface, how to extend .NET class in Java and how to build custom strongly-typed wrapper for .NET object. Javonet allows you to create WinForms window the same way as you would do it in .NET by extending “Form” class. Using built-in events bridge you can subscribe to .NET events and provide fully functional interactive interface.
Use cases
This solution might be helpful in each situation where you have business logic implemented in Java and just wants to expose the user interface using .NET, when you plan to migrate your Java application to .NET or if you have another system built on Java which forces you to develope user logic in Java but you would like to levarage possiblities of WinForms interace.
Prerequisites
To build Java application with WinForms interface all you need is Javonet developer license and Javonet desktop license for each end-user workstation.
Step by step instruction
1) Before using Javonet it’s good practice to prepare Javonet XML configuration file first. XML configuration file allows us to provide license details, apartment state and assemblies to be referenced in easy to access and modify way. Using XML config files it’s not required to activate or configure Javonet from code, just by first usage of Javonet API it will configure itself automatically.
Our XML configuration file will look like the one presented below. We will use two .NET libraries from .NET framework, therefore in the “reference” section we add “System.Windows.Forms” and “System.Drawing”. Notice that we provide just name of the library not the full path, this way Javonet will use library located in GAC.
One of the most important change is ApartmentState, this value indicates in what kind of apartment state our .NET process main thread will be running, to create WinForms window it should be set to STA (Single Thread Application).
Save the XML presented above in “javonet.xml” file.
2) Now we can create our Java Project in Eclipse using menu “File > Java Project”. Give the name “JavonetWinformsApp” and press “Finish”.
3) When our application is ready we have to copy “javonet.jar” and “javonet.xml” (prepared in first step) to our new project. Next right click on your project go to “New > Class” and in the “New Java Class” window provide name “JavonetWinformsAppMain” and check field “public static void main(String[] args)” so the default entry method will be created. Next create another Java Class without “main” method, called “CalculatorForm” this class we will use to extend “Form” and implement our WinForms form logic. Our project should look like this:
WinForms Sample Project
4) Next step is to add “javonet.jar” file reference to our project. Right click on the project choose “Build Path > Configure Build Path” and in the new window go to “Add JARs” and choose “javonet.jar” from the our project. Accept all windows and close until you will get back to the project view.
New item in our project should appear called “Referenced Libraries” with javonet library inside.
5) Now we can build our application. We will start with CalculatorForm. The first step is to tell Javonet that our class “CalculatorForm” will be extending .NET “Form” class. To do it extend NObject class and add default constructor in which you will call base constructor providing type of .NET class to be extended.
Because during initialization of base .NET type any .NET exception can occure our constructor must declare that it can throw JavonetException which wraps any .NET side exception. As you see in call to base constructor by “super” operator we pass as argument string constant with full name of .NET type which will be extended by our Java class. From now our CalculatorForm class can access any protected and public fields and methods of “Form” class using Javonet notation. In this class we will implement our interface design and logic.
Let’s create standard “InitializeComponents” method where we will specify details about our window design. This method will be called from our constructor:
Now our code works pretty the same as .NET WinForms window definitione. We have added “tbDisplay” TextBox variable on class level (as it is instance of .NET object we hold it in NObject variable). Next we added “InitializeComponents” method which we call from constructor. In this method we call set on local “Text” and “Width” fields of our .NET window and we initialize new instance of TextBox for which we set several properties and font. Please notice that we use .NET enum field to specify HorizontalAlignment of our text and we also initialize .NET font object which we pass as reference to “Font” field on our TextBox object. At the end we get Controls property of our Form and invoke “Add” method passing our TextBox instance as argument.
Now we are almost ready to run our first WinForms window. The only missing part is “ShowDialog” or “Show” method which we can wrap in our “CalculatorForm” class so it will be accessible as strongly-types method in our Java application. This is good example of preparing custom wrapper classes if you would like to expose .NET objects in strongly-typed manner for your Java applications. Such wrapping method should look like this:
Our wrapping method is called the same as .NET counterpart and accepts exactly the same arguments (in this case no arguments). Now any other class in our Java application can call “ShowDialog” method on our Form like it was regular Java method. This one we will use in our main method at startup of our application:
The final result of executing our application should be as follows:
Javonet WinForms Calculator Form
And that’s it we have real WinForms window initiated by Java application by extending .NET Form class in Java. Now we can add some buttons and event listeners to handle click events. We will declare buttons variables as NObject field on class level and initialize them in our “InitializeComponents” method:
Here you can see how our button initialization looks like. We initialize new Button object, set some properties and add new Event Listener for “Click” event. There are several different ways for subscribing .NET events about which you can read more in our Quick Start Guide. This example uses anonymous class. While .NET event occurs our “eventOccurred” method is called. As you see .NET event arguments are delivered in Object array but we split them into regular arguments in our event handler method “button1_Click”. As arguments are “Object sender, EventArgs e” so both are .NET instances our event handler accepts as arguments two NObject variables. Our event handler method looks like this:
Implementation of “NumberPressed” method is simply displaying pressed numer and performing some simple calculation:
Now our application has new button “1” and after pressing this button the digit “1” is appended to our display. Now we can specify rest of the controls and finalize our calculator application written in Java with WinForms interface. On the image below you can see the final result. For full application source code please download our full sample.
Javonet WinForms Calculator Window
Download Sample Project
Use the links below to download full source code and binaries of the sample project created in this tutorial:
Download Source Code and Binaries
Java Swing or Windows Forms for desktop application? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago .
I am writing a fat client application that I would ideally like to be cross-platform, but may settle for Windows-only based on the following:
- I am much stronger in C# than Java (though I have a few years Enterprise Java experience)
- I haven’t done much with either Windows Forms or Java Swing, just toy apps for each, though I’m more comfortable in Windows Forms than Swing
- I am concerned about the performance of Swing applications (vs. Windows Forms), but I’m wondering if things have improved in the last couple years
- I am completely open to developing in either platform. no MS or Java snobbery here. In fact, if you want to tell me to do it in Qt, wxWidgets, or something else, I’ll listen!
If any of you switched from Windows Forms to Swing, what were the biggest hurdles?
Was the initial appeal of being cross-platform worth it in the end, or would a faster, less flexible Windows-only app have been better?
If you use Java Swing, which IDE do you prefer for drag/drop form layout?
8 Answers 8
I develop a complex low latency GUI in a bank using the Netbeans RCP platform. There are a lot of people using Netbeans. I also work with WinForm devs too.
The WinForm guys have some extremely rich and high performance/low footprint grids and widgets, especially commercial offerings such as Infragistics. Netbeans apps can have large resource footprints so it really needs to be something your customer is spending most of his computing resources on.
If it needs to be lean and interface rich or fast then Winforms is the way to go.
If you want a large library of APIs to build out and maintain a large complex GUI codebase and have the freedom of a completely portable tech stack I would choose Java everytime.
Having said that, Netbeans IDE is appalling! You would do well to visit Geertjan’s blog if you go for it.
Check-in your code regularly to a branch when using the Matisse Graphics editor, it often goes nuts and trashes your layouts and fails to undo. The underlying RCP platform dependancy handling -if it gets you in a twist — can be challenging, but still better than OSGi!
Good luck, GUI development isn’t as easy as people say!
What is the Java equivalent to C#’s Windows Forms for building GUI apps easily and rapidly
I wanted to learn to program and looked at both Java and C#. I decided to go with C# because it was so easy to just open a form and plop some buttons and text boxes on it. With just one download, C# Express got me going.
From what I saw with Java that couldn’t be done. At least not without downloading and configuring other software. All of which I found no easy instructions for. But I really wanted to learn Java.
I really don’t want to learn how to create UIs AND a new language. I’d rather just to concentrate on the code itself. Is there an easy way (like C# basically) to create Java apps?
12 Answers 12
Are you talking about a GUI builder?
There are many GUI builders out there. Some IDEs like Netbeans come with nice GUI builders for Java.
There are options in the Java community for ‘one-stop downloads’ like you mention. They are out there, they’re just not as public as Visual Studio is. EasyEclipse is one of my favorites.
I’ve done a fair bit of work (> 4 years) on both platforms and the biggest difference I have found is not so much with the languages per se, but with the whole development environment. Specifically:
In .Net (be it ASP.Net, C# or VB.Net) the examples you find on MSDN or elsewhere tend to just work without a whole lot of tweaking, and the documentation is more reliable, whereas Java examples — especially the open source ones — often don’t work out of the box and the documentation (yes, Apache Slide and Shindig — I’m looking at YOU!) is sometimes non-existent.
Having said that, the sheer amount of choice for plugins, libraries and the like for the Java platform is enormous, which is unsurprising considering its (largely) non-proprietary nature.
It seems to me the best and easier way to get what you want is to use Netbeans (JDK 6u14 with NetBeans IDE 6.5.1 Bundle).
It has both the JDK and the IDE in one download. And it has a easy and very good GUI builder.
Java is a very good platform and as you said it is easier to create cross platform applications, however, don’t except this from the Desktop UI which, although still cross platform, not always gets right on all platforms with out modification.
You would have to add some code specific for each target desktop, but definitely that’s a lot easier than write three different application ( One in Visual C#, other in GTK, and other in Cocoa )
I been learning to program in C# for a few months, but recently I signed up for a course which required Java. So I started playing around with java, first tried Netbeans then Eclipse.
I’m kinda a computer programming noobie (background mainly in web design) so take my perspective as a student. After messing around with Java for a bit i still prefer C#. While both are excellent the factors that tipped C# in my case are as follows
- Easier documentation and a large variety of easy to view Video Tutorials, yes I know netbeans have a very large collection of video tutorials as well, they are encoded at such a terrible resolution and bitrate that they are largely unwatchable
- Slightly easier to learn, maybe it’s cause of the IDE or the syntax, but I found C# easier to pick up.
- More functionality exposed right after using the IDE, you will find more controls in visual studio than compared to Netbeans. Eclipse was quite confusing for a noobie like me wading through it’s massive plugins.
I think the only thing java has going for it, is that it’s supported on more platforms. Though Mono is making up for the linux and Apple side so i guess it’s not that bad. Either way they are both extremely productive environments, just in my humble noobie opninion C# is just slightly more intuitive to pick up.