Create GTK based Java applications on Linux with Java GTK library

Thanks to   libjava-gnome-java   – Java-gnome language bindings project – library, you can create java applications based on GTK toolkit for GUI displaying, instead of Java Swing. GTK has the widgets you need to build from basic to complex GUI.

First of all, install the library. If you’re running Ubuntu Linux or Debian, you can do it this way:

sudo apt-get install libjava-gnome-java

The library should be installed in: /usr/share/java/gtk.jar

You can therefore import it to your java project’s classpath in your favorite IDE, along with the javadoc (install libjava-gnome-java-doc as well).

Hello_Java_GTK

Hello_Java_GTK

The example above is a basic GTK window as illustration that you can achieve with the following portion of code:


package com.otika.samples;

/**
 * Basic example of how one can use Java Gnome Library to build a GUI.
 *
 * @author <a href="mailto:hanynowsky@gmail.com">Hanynowsky - OTIKA</a>
 */
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.gnome.gdk.Event;
import org.gnome.gdk.Pixbuf;
import org.gnome.gtk.*;

public class HelloGTK extends Window {

Window window;
 Dialog Hello;
 Label label;
 VBox vbox;
 Button button;
 Dialog msg;
 Button but;
 Button exit;
 String message = "Java Gnome Library is great!";
 Alignment align;
 Alignment align2;
 AboutDialog about;
 Pixbuf icon;
 File pic = new File(System.getProperty("user.home") + File.separator + "linux.png");

/**
 * COnstructor
 */
 public HelloGTK() {

try {
 pasteLogoFile();
 align = new Alignment(0, 0, 0, 0);
 align2 = new Alignment(0, 0, 0, 0);
 msg = new MessageDialog(null, true, MessageType.INFO, ButtonsType.OK_CANCEL, "Change text to -" + message + "- ?");
 Hello = new InfoMessageDialog(null, "GTK Modal Dialog", "Thanks to Java Gnome Library I exist");
 window = new Window();
 window.setTitle("GTK Window");
 window.setDefaultSize(390, 400);
 window.setBorderWidth(6);
 icon = new Pixbuf(pic.getPath(), 48, 48, false);
 window.setIcon(icon);
 window.connect(new Window.DeleteEvent() {

@Override
 public boolean onDeleteEvent(Widget source, Event event) {
 exit();
 return false;
 }
 });

exit = new Button("Exit");
 exit.setSizeRequest(70, 30);
 exit.connect(new Button.Clicked() {

@Override
 public void onClicked(Button source) {
 exit();
 }
 });

label = new Label("This window has been made with the <b>java-gnome</b> "
 + "project.\nTo get more information, you should check out the"
 + "\ndocumentation.");
 label.setUseMarkup(true);
 vbox = new VBox(true, 5);
 vbox.setSizeRequest(window.getWidth() - 2, window.getHeight() - 2);
 vbox.setBorderWidth(0);
 but = new Button("Click It");
 but.connect(new Button.Clicked() {

@Override
 public void onClicked(Button source) {
 showIt();
 System.out.println("Clicked it");
 }
 });
 button = new Button("Click me");
 button.setRelief(ReliefStyle.HALF);
 button.connect(new Button.Clicked() {

@Override
 public void onClicked(Button source) {
 showMe();
 }
 });
 vbox.packEnd(but, false, false, 0);
 vbox.add(label);
 vbox.packStart(exit, false, false, 0);

 MenuBar menuBar = new MenuBar();
 MenuItem fileItem = new MenuItem("File");
 menuBar.append(fileItem);

Menu quitMenu = new Menu();
 MenuItem quitItem = new MenuItem("Quit");

quitItem.connect(new MenuItem.Activate() {

@Override
 public void onActivate(MenuItem menuItem) {
 exit();
 }
 });

quitMenu.append(quitItem);
 fileItem.setSubmenu(quitMenu);

vbox.packStart(menuBar, false, false, 3);
 about = new AboutDialog();
 String[] people = {"Hanynowsky <hanynowsky@gmail.com>", "Gnome <www.gnome.org>"};
 about.setArtists(people);
 about.setAuthors(people);
 about.setCopyright("@ 2012 - Copyright");
 about.setLicense("GNU GPL v3");
 about.setProgramName("Java Gnome Hello World");
 about.setWebsite("https://hanynowsky.wordpress.com");
 about.setLogo(icon);
 about.setVersion("1.0");
 about.setModal(true);
 Fixed fixed = new Fixed();
 Button aboutMe = new Button("About");
 aboutMe.connect(new Button.Clicked() {

@Override
 public void onClicked(Button source) {
 about.run();
 about.hide();
 }
 });
 aboutMe.setSizeRequest(70, 30);
 fixed.add(aboutMe);
 vbox.packStart(fixed, false, false, 0);
 HSeparator hsep = new HSeparator();
 vbox.packStart(hsep, false, false, 0);
 // vbox.add(exit);
 String s = "コーヒーとコードを飲む。";
 String p = "بطريق";
 vbox.add(new Label(s));
 vbox.add(new Label(p));
 vbox.add(button);
 align.add(vbox);
 window.add(align);
 window.showAll();
 } catch (IOException ex) {
 Logger.getLogger(HelloGTK.class.getName()).log(Level.SEVERE, null, ex);
 }
 }

public static void main(String[] args) {
 Gtk.init(args);
 HelloGTK hw = new HelloGTK();
 System.err.println("Running The GTK Window");
 hw.showWindow();
 // Gtk.main();
 }

private void showMe() {
 // msg.run();

ResponseType response = msg.run();
 if (response == ResponseType.OK) {
 label.setLabel(message);
 msg.hide();
 } else {
 msg.hide();
 }
 }

private void showIt() {
 Hello.run();
 Hello.hide();
 }

private void showWindow() {
 Gtk.main();
 }

/**
 * Copies a logo file in user home folder
 */
 private void pasteLogoFile() {
 if (!pic.exists()) {
 try {
 InputStream is = getClass().getResourceAsStream("linux.png");
 pic.createNewFile();
 try (OutputStream output = new FileOutputStream(pic)) {
 int read;
 byte[] bytes = new byte[1024];
 while ((read = is.read(bytes)) != -1) {
 output.write(bytes, 0, read);
 }
 is.close();
 output.flush();
 }
 System.out.println("Logo created");
 } catch (IOException ex) {
 Logger.getLogger(HelloGTK.class.getName()).log(Level.SEVERE, null, ex);
 }
 }
 }

/**
 * Exists the application
 */
 public void exit() {

if (pic.exists()) {
 pic.delete();
 }
 Gtk.mainQuit();
 }
}

You can download and run the example using this jar file :

on terminal :

java -jar /path_to_this_jarFile/HelloGTK.jar
Advertisements

2 thoughts on “Create GTK based Java applications on Linux with Java GTK library

  1. Ghaydaa Kalla

    Today, I went to the beachfront with my children.

    I found a sea shell and gave it to my 4 year old daughter
    and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is entirely
    off topic but I had to tell someone!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s