Getting Started with Java + GTK

You can build Java applications that interface "better" your Linux desktop through the use of Java GTK. At the time of this writing, vertsion 4.1 is out


    Install Java if you haven't already
    Install the java-gnome packages which will install the gtk to /usr/share/java
    sudo apt-get install libjava-gnome-java
    In your favourite text editor, copy and paste the following and save it as
    import org.gnome.gdk.Event;
    import org.gnome.gtk.Gtk;
    import org.gnome.gtk.Widget;
    import org.gnome.gtk.Window;
    import org.gnome.notify.*;
    import org.gnome.notify.Notification;
    public class Main 
        public static void main(String[] args) 
            // gtk init
            // create main window
            final Window window = new Window();
            // connect delete event to main window
            window.connect(new Window.DeleteEvent() {
                public boolean onDeleteEvent(Widget source, Event event) {
                    return false;
            // create notification
            Notify.init("My Application");
            String iconPath = "/full/path/to/your/icon.svg";
            Notification notification = new Notification("Notification Title", "message", iconPath);
            // show widgets
            // main loop
    You will need to change the iconPath variable to point to one of your icons if you want to see an icon in the notification that will pop up when you run the program.
    Now open a terminal and navigate to where you saved the file and run the following commands:
    javac -classpath $CLASSPATH:/usr/share/java/gtk.jar
    java -classpath $CLASSPATH:/usr/share/java/gtk.jar Main
    Things are much easier if you use an IDE like Netbeans to manage the GTK library, but I wanted to cater for users who don't yet have an IDE. I also did not want to go into the specifics for Netbeans and Eclipse users.
    That's it! You should have seen a notification in the top right of your screen pop up, as well as window that you can close which had nothing inside it.


No comments:

Post a Comment