A day in the life of Danny D'Amours

Creating a GTK app in Java using java-gnome

1) Install java-gnome

On a Fedora 7 system : run “yum install libgnome-java” which will bring in glib-java, libgtk-java and perhaps cairo-java packages.

2) In Eclipse create a new Java project.

3) Add the java-gnome jar files to the project (on Fedora 7 these are gtk2.8.jar, glib0.2.jar and gnome2.12.jar in the /usr/share/java directory). Note that in this example, the gnome2.12 jar file is not actually required.

Java GTK app #1

4) Enter this code:

package net.damours.danny;

import org.gnu.gtk.Button;
import org.gnu.gtk.Gtk;
import org.gnu.gtk.Window;
import org.gnu.gtk.WindowType;

public class GWTExample {

public static void main(String[] args) {

// Initialize the GTK subsystem

// Create a Top Level Window
Window w = new Window(WindowType.TOPLEVEL);
w.setDefaultSize(100, 100);

// Create a button and add it to the window
Button b = new Button(“Hello”);
// Show the window and all subcomponents

// Run Gtk main thread
Java GTK app #2

5) Click on the run button.

Hopefully you will see something like the following:
Java GTK App

Even though there is no functionality behind the window and button, you now have a GTK application written in Java which can be built upon. Since the java-gnome jar files rely on the underlying GTK libraries being available, I don’t believe that this will work on any platform but Linux but I would love to be proved wrong.

The next step will be to add functionality and exploit some more advanced features of the java-gnome bindings.

Related Posts:

August 14th, 2007 Posted by Danny D'Amours | Eclipse, Java, Linux, Tech | one comment

1 Comment »

  1. Thank you, I wasn’t able to figure where my Linux system stored the jars. 🙂

    Comment by Daniele Verducci | March 26, 2014

Leave a comment