Developing GNOME Applications with Java

Design your application's GUI look in XML, write the code in Java and plug the whole thing in to the GNOME desktop.

Listing 7 displays some of the corresponding Java source code for GnomeSesameFormat. Listing 8 contains a portion of GnomeSesameFormat's interface definition.

GnomeSesameFormat is a simple application I developed, and most of its work is done by executing an external program called sesame-format. sesame-format formats a disk to contain an encrypted filesystem. GnomeSesameFormat simply provides a GUI wrapper for this command-line tool. GnomeSesameFormat can be executed with its --dry-run option to facilitate testing and experimenting. As of this writing, it's probably a bad idea to format a disk using this tool. A screenshot of GnomeSesameFormat is shown in Figure 5.

Figure 5. GnomeSesameFormat gives you an easy-to-use front end for setting up and using encrypted disk partitions.

The GnomeSesameFormat application is implemented in a single class, GnomeSesameFormat. The GnomeSesameFormat class' main function initializes the GTK libraries using the Gtk.init method, creates a new GnomeSesameFormat instance and releases control to the GTK event loop by calling Gtk.main.

The interesting work begins in the GnomeSesameFormat class' constructor. In the constructor, a LibGlade object is instantiated. It reads a GLADE user interface description and instantiates its corresponding objects. A reference to these objects can be retrieved by name using the LibGlade object's getWidget method. Once we have a reference to an interface component, we can use them as if we created them ourselves. The GnomeSesameFormat class also contains the signal handling methods referenced in

In developing GnomeSesameFormat, I used the four steps presented above. For example, a button was defined using GLADE as part of the application's GUI (step 1). The button was named buttonFormat (step 2). Again using GLADE, a method name of onButtonFormatClicked was designated to handle the button's clicked symbol (step 3). Finally, the onButtonFormatClicked method was implemented in GnomeSesameFormat's Java source code (step 4).

In order to manipulate components further, libglade can provide a reference to an individual component. A LibGlade object's getWidget method provides this capability. To illustrate this, we can investigate GnomeSesameFormat's errUI component. The errUI component is a Window that displays error messages for the user. The errUI window was defined in GLADE (step 1) and named (step 2). Because we know the name of errUI, we can get a reference to it by calling getWidget(errUI). Once we receive a reference to the component, any GTK method may be invoked. GnomeSesameFormat uses errUI's show and hide methods.

The GNOME Project provides the ability to develop applications in C, C++, Java, Python and Perl. In addition, external projects such as Mono provide even more diversity. When used with several of these alternatives, the GLADE User Interface Builder makes it possible to write applications quickly with a graphical user interface for the GNOME platform. Once the graphical components are defined, an application shell and signal handlers all are that remain to be implemented. This implementation can be done using any programming language.

Resources for this article: /article/8274.

Mike Petullo currently is working at WMS Gaming and pursuing a Master's degree at DePaul University. He has been tinkering with Linux since 1997 and welcomes your comments sent to Thank you to Noah Alcantara for helping to review this article.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

need to develop a software in java

Anonymous's picture

hey buddy
i need to make a software for a billing to a customer suggest me a tool

Makefiles please

John Ellson's picture

There are no compile instructions in the article for GnomeSesameFormat, and the rest (except for HelloWorld) don't seem to work for me on latest Fedora-Core development.

Could you add a simple Makefile to your resources? Perhaps one for FC3 and another for FC4?

is libglade available in Windows?

theNeo's picture

Loading GUI from a single descriptive .glade file is quite interesting... could I use it in my win32 pygtk app?

Yes indeed you can. Glade

Christian Kaas's picture

Yes indeed you can.

Glade GUI files are brilliant in the way that you can reuse them for implementing an application in various programming languages (if needed). For instance in C/C++/C#, Python, Java.. and without making changes to the glade GUI file.

Brilliant that is!

Both Glade and Libglade are available on windows

Anonymous's picture

Both Glade 2 and Libglade are available on windows

Re:Both Glade and Libglade are available on windows

Michael Bonder's picture

Yepp - excellent. Now I can use my Laptop, when I go on a holiday-trip. On this machine I am missing some special drivers ... so I am stuck to Windows.

This article claims that Exam

Rob's picture

This article claims that and were compiled with gcj, for example:

"Listing 3 can be compiled with gcj --main=ExampleSwing -o ExampleSwing"

I don't think anyone has implemented a complete open source version of Swing yet, and it certainly doesn't work out of the box. Try it.

Try it yourself.

Anonymous's picture

No, the Swing support in GCJ is not 100% yet. That doesn't mean it's not far along enough to be able to run that simple example.

Try it yourself. (And consider upgrading if it doesn't work.)

What about windows?

Jeff's picture

There's a windows compilation howto here:

However, this is non-free and non-native, as it still uses a JVM and .class files. Does anyone know how to use gcj to create native binaries of java-gtk/java-glade programs for windows?

Cross-compiling is fine, setting up Mingw/MSYS like the article suggests is fine, but nothing I've tried thus far can generate an executable like gcj on linux is doing.

it's free & native

undefined's picture

i just completed the instructions using the "current" (as of earlier this week) MinGW, MSYS, & msysDTK.

there are a few gotchas not documented in the instructions (maybe due to different versions of MinGW), but otherwise the building of libgtk-java & libglade-java went okay.

to test the build i ran an example from each package (gtk & glade) using both gcj (bytecode & native) & gij.

the java-gnome instructions for native java compiles reference shared libraries, but as java-gnome won't build as shared libraries on win32, the instructions obviously can't apply to win32. here documents how to build DLLs, but i couldn't get that to work either (though i think the examples used there are all pure java, no C as with gtk, i believe). i can build huge monolithic native executables (5 MB stripped for one of the gtk example applications) by just referencing all needed jar files during the compile (and don't forget "-fjni"), but that's ridiculous (and doesn't allow for commercial applications as LGPL libraries, such as gtk, can only be compiled against as shared libraries without being LGPL or GPL).

the script doesn't work for me, so to run an example "by hand", after following the build instructions:

export PATH=/mingw/bin:/target/bin:/usr/local/bin:$PATH
export CLASSPATH=/mingw/share/java/libgcj-3.4.2.jar:/usr/local/share/java/gtk2.6-2.6.2.jar:/usr/local/share/java/glade2.10.jar
cd /usr/local/src/libgtk-java-2.6.2/doc/examples
gcj -C testgtk/
gij testgtk.TestGTK
cd /usr/local/src/libglade-java-2.10.1/doc/examples
gcj -C glade/
gij glade.Test

maybe the script needs to be patched to properly handle MinGW's gcj/gij. maybe i'll do that... tomorrow.

Do it in C or Python

Anonymous's picture

And don't expect Linux or Gnome users to use your java app.

Whats with the anti-Java attitude

speedy's picture

im sick of people's attitude towards Java if you use gnome libs to build gui instead of swing then you dont have to stick with Suns vm or interpreter.
C wont give you a significant speed advantage and development of usable good code will be slower cause things a simply more lowlevel.

Python is supposed to have a much more beautiful and elegang syntax which might be true but many developers have experience with Java and dont like to learn a new syntax just because some ppl are running crusades against their language. And besides that Python is slow,, ppl always yap about java being slow its not,, a lot have changed since Java 1.3 yes, swing maybe slow and some stuff like geometry have some left to be desired, besides that Java will give C a run for its money
in several types of apps, no im not saying that stuff like the kernel
should be made in Java but desktop apps could be made just as good and
development would just be faster with Java. Python on the other hand is dead slow run psycho on it i dont care. In almost every perfooormance bench it LOSES.

support java, but don't bash python

undefined's picture

i find it humorous that you respond to someone bashing java by bashing python. yes, the original poster tried to laud c & python over java, but that doesn't mean you have to lash out against python, because now you have shown the same narrow-mindedness as the original poster (just with a different language ignorance/prejudice).

many developers familiar with both java & python have documented their experience on the net, and many have said that python is prefered for various reasons, so there is something to be investigated there. but that is just those people's opinions, and of course it's not better than java in ALL situations (use the "right", most appropriate, tool for the job).

most humorously, the major reason python is touted over java is the reason you give for java over c: python is even more high-level than java, meaning higher productivity.

i'm interested in this article (though read in my dead-tree copy, i stumbled across the online edition searching for "java gtk glade" as i don't want the gnome dependency on linux and it's not available for windows) because i want to experiment with glade in both pygtk & java-gtk, while expanding my python knowledge and learning java (as a 10-year c++ veteran).

That users (either you) wont

Anonymous's picture

That users (either you) wont even know it was coded in Java :-)))

Thats the most nice thing about gcj... creating native binaries.

a question

Jeff's picture

One question? Does the .glade file have to be distributed with the compiled application, or is it statically included in the compiled binary?

For example, in Listing 5, would I need to ship with the compiled app?

Last time I was checking on G

Anonymous's picture

Last time I was checking on Glade's progress it was already possible to load .glade from anywhere - effectively in-memory array.

So in the end you can have single executable with no external dependencies.

On other side, if you are talking about real-world distribution, it doesn't matter at all: end-user got only .deb or .rpm or whatever.
After all, license text must be included as well ;-)

Re: a question

Anonymous's picture

No, the .glade file is not required to run the compiled binary. It is used while designing your GUI (in glade) and brought in during compilation.

Not if you use LibGlade, AFAI

Anonymous's picture

Not if you use LibGlade, AFAIK. And LibGlade is recommended now, instead of C code generation...

Great article! If you comp

Anonymous's picture

Great article!

If you compile java code to native code with gcj, it's a good idea to remove the symbols from the resulting object file, e.g.

strip HelloWorld

Reduces the size of the binary significally.

Hello-World is Cult 8-)

Jackke's picture

The "hello world" lines are cult and have to begin every programming-lesson 8-)))))))

This is a great article!

Anonymous's picture

This is a great article! Thanks for the great tutorial as well as information on the differences between AWT and Swing.

Is there a way, however, to compile this almost like a Makefile where the libraries are automatically found via "pkg-config" or "pkgconfig" which can be saved in variables which are called during the actual compile?

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Upcoming Webinar
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
11am CDT, April 29th
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot