The helpful java.awt.Desktop class

I am currently working with Jörg on the betsy tool which compares the WS-BPEL 2.0 conformance of open source WS-BPEL 2.0 engines. At the end of a successful comparison run, html reports are generated and can be opened by the user via the browser. I often had to execute such a betsy run in different configurations and wanted to look at the results only. The problem is that you have to wait until the run is completed (no notifications available), open the windows explorer, navigate to the correct folder and open the html file. This process can be shortened by using bookmarks in the browser for the results. However, this does not solve the problem on being notified after the run is completed. Regardless, I have found a very simple solution: the java.util.Desktop class.

This class is shipped with the Java SE edition since Version 6 and can be used to open the default browser at a specific URI, open the default mail application (even open the compose mail window) or open/print a specific file using the registered application for this file type. I created a few examples in groovy code to show you how simple this functionality can be used as mostly only one import statement and one code statement is required. The code is written in groovy, however, it is also compatible with Java code. Just copy the import statement to the top of the Java class and past the other code line where you want to execute it.

Moreover, you can open a file using the default editor, too. As some operation systems may not support this class, you can check with the static method Desktop.isDesktopSupported() whether it is possible to use this functionality in general. For more information using this class, please refer to the Javadoc documentation.

How to create a java GUI quickly?

I had to create a simple Java GUI for my diploma thesis. So I asserted, how I can do this. After quite some research, I found a solution which reduces the amount of code needed using some of the best technologies in combination:

1. GUI Tool

In order to create the GUI, WindowBuilder Pro should be used. It was a commercial software from Instantiations. However, Instantiations was bought by Google and all their tools have been released under an OpenSource License and are now available for free.

2. Layout

Currently, the best layout manager for a Swing GUI is MigLayout. It is announced to be inside JDK7. This layout works seamlessly in the WindowBuilder Pro.

3. Controller and Models

For the implementation of the Controller and the models, I suggest Groovy. This reduces the amount of Code needed for this kind of work.

4. Binding

Then, the model elements have to be associated to GUI elements back and forth. For this purpose, WindowBuilder Pro uses the BeansBinding JSR. This is not worked on anymore, but does work alright. In combination with Groovy, it is really very very easy to do. Just use the editor for setting up the bindings and annotate the Groovy Class with @Bindable. For Lists use the ObservableList from the BeansBinding Jar.

Summary

Using Groovy (with @Bindable) and WindowBuilder Pro, I have built an amazing Swing GUI in no time. With really, really small amount of code. You should try it out, too!