Tuesday, 3 August 2010

Java and Gnome

I don't often write GUI applications but I have been experimenting over the last few days with the Java-Gnome User Interface Library for writing Gnome-based applications in Java.

The disadvantage of this of course is that you forego the "write-once-run-anywhere" ethos behind Java; the finished application will only run on a system that uses Gnome as its window manager. The standard, cross-platform way of doing things is to use Swing but the "native" look & feel widgets are only imitations of the real thing and, Windows aside, can look like poor imitations.

Compare these screenshots for example, of a colour chooser dialog in Swing on a Gnome desktop (Ubuntu 10.04) and a native Gnome application:

I suppose the best compromise would be to use the Standard Widget Toolkit (SWT), as used by the Eclipse platform, which is a standard interface underpinned by native windowing system widgets.

The Java Gnome User Interface Library

Subject to the above caveats, my first impressions of the Java-Gnome UI Library is very good. I don't think all of GTK is covered as yet, but there is certainly enough there to write perfectly serviceable applications for Gnome desktops. As an exercise, I managed to knock together a perfectly serviceable text editor in a couple of days, complete with syntax highlighting, cut/copy/paste, undo/redo, open/save dialogs and shortcut keys.

I found the code quite simple to write and was up and running quite quickly. Creating things like menus and toolbars is made very easy with the concept of actions, and standard framework actions (stock actions) keep things consistent. Handling events is straightforward when you have written a couple of event handlers. The trickiest part was getting round a few subtleties of the undo/redo mechanism, which kept crashing the application because I didn't understand it properly. But rock solid now.

As with any GUI framework, code can get into a mess very quickly, so some organisation is essential. The approach I used on the editor exercise was to make the main application window a subclass of the Window class, set it up as a singleton and have it create the other components it needed in its constructor. Any code in the components can then look up the singleton instance and access methods and data in the other components in a controlled way.

There is an excellent tutorial available to get started and the API documentation is very good.

Well worth a try if you want to write some applications for the Gnome desktop in Java.

No comments: