Developing GNOME Applications with Java
Listing 6. ExampleGNOME.glade Fragment
<?xml version="1.0" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM
"http://glade.gnome.org/glade-2.0.dtd">
<glade-interface>
<requires lib="gnome"/>
<widget class="GtkWindow" id="ExampleGNOME">
<property name="visible">True</property>
<property name="title" translatable="yes">
GNOME</property>
<property name="type">
GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">
GTK_WIN_POS_NONE</property>
<property name="modal">False</property>
<property name="resizable">True</property>
<property name="destroy_with_parent">
False</property>
<property name="decorated">True</property>
<property name="skip_taskbar_hint">
False</property>
<property name="skip_pager_hint">False</property>
<property name="type_hint">
GDK_WINDOW_TYPE_HINT_NORMAL</property>
<property name="gravity">
GDK_GRAVITY_NORTH_WEST</property>
<child>
<widget class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
...
<child>
<widget class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<widget class="GtkButton" id="yesButton">
<property name="visible"> True</property>
<property name="can_focus">True</property>
<property name="label">gtk-yes</property>
<property name="use_stock">True</property>
<property name="relief">
GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">
True</property>
<signal name="released"
handler="on_yesButton_released"
last_modification_time=
"Sun, 21 Nov 2004 19:10:01 GMT"/>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">False</property>
</packing>
</child>
...
</child>
</widget>
</child>
</widget>
</glade-interface>
The GLADE system provides a User Interface Builder that makes creating definitions such as ExampleGNOME.glade simple. Figure 4 shows an example GLADE User Interface Builder session. Listing 8 contains some of the interface description being edited. Essentially, GLADE allows you to create a user interface component, name the component so it can be referenced by the corresponding program, provide method names for component signal handlers and define various properties for the component.
Designing the GUI using GLADE and allowing libglade-java to do the heavy lifting significantly reduces the work of an application developer.
Listing 7. GnomeSesameFormat.java Fragment
public class GnomeSesameFormat {
...
private void init() throws IOException {
glade = new LibGlade(System.getProperty("GLADE_FILE"), this);
// Default values.
isDryRun = false;
cipher = new
AES256();
fs = new Ext3();
passphrase = null;
volName = null;
// References to various windows used by
// application.
topLevel = (Window) glade.getWidget("topLevel");
devSelUI = (FileSelection) glade.getWidget("devSelUI");
errUI = (Window) glade.getWidget("errUI");
progressUI = (Window) glade.getWidget("progressUI");
}
...
public GnomeSesameFormat() throws IOException {
init();
device = null;
Label l = (Label)
glade.getWidget("displayedDevice");
l.setText("none selected");
}
...
public void onFormatButtonClicked(GtkEvent event) {
Entry entry;
entry = (Entry)
glade.getWidget("entryPassphrase");
passphrase = entry.getText();
entry = (Entry) glade.getWidget("entryVolumeName");
volName = entry.getText();
if (topLevelInputOk ()) {
ProgressBar p = (ProgressBar) glade.getWidget
("progressBarFormat");
Label l = (Label) glade.getWidget("labelFormat");
ProgressBarUpdater pU = new ProgressBarUpdater(p);
topLevel.setSensitive(false);
progressUI.show();
if (! isDryRun) {
l.setText("Formatting " + device);
pU.start();
execSesameFormat();
pU.stopReq();
try {
pU.join();
}
catch (java.lang.InterruptedException e) {}
} else {
l.setText("[Simulated] Formatting " + device);
pU.start();
try {
Thread.sleep(1000);
} catch (java.lang.InterruptedException e) {}
pU.stopReq();
try {
pU.join();
} catch (java.lang.InterruptedException e) {}
}
progressUI.hide(); topLevel.setSensitive(true);
}
...
private void error(String msg) {
Label l = (Label) glade.getWidget("labelErr");
l.setText(msg);
topLevel.setSensitive(false);
errUI.show();
}
public void onErrOkButtonClicked(GtkEvent event) {
errUI.hide();
topLevel.setSensitive(true);
}
...
public static void main(String args[]) {
GnomeSesameFormat gui = null;
Gtk.init(args);
LongOpt[] longOpt = new LongOpt[2];
longOpt[0] = new LongOpt("help",
LongOpt.NO_ARGUMENT, null, 'h');
longOpt[1] = new LongOpt("dry-run",
LongOpt.NO_ARGUMENT, null, 'd');
Getopt g = new Getopt("gnome-sesame-format", args,
"hd", longOpt);
int c;
boolean optDryRun = false;
while ((c = g.getopt()) != -1)
switch (c) {
case 'h':
printUsage(0, null, null);
case 'd':
optDryRun = true;
break;
default:
printUsage(1, null, null);
}
try {
int i = g.getOptind();
if (i == 1)
gui = new GnomeSesameFormat(args[i]);
else if (i > 1)
printUsage(1, null, null);
else
gui = new GnomeSesameFormat();
gui.setDryRun(optDryRun);
Gtk.main();
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
...
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- New Products
- Paranoid Penguin - Building a Secure Squid Web Proxy, Part IV
- Developer Poll
- Trying to Tame the Tablet
- Looking Good
7 min 9 sec ago - Hey God - You may not be
4 hours 20 min ago - Reply to comment | Linux Journal
6 hours 53 min ago - Drupal is an Awesome CMS and a Crappy development framework
11 hours 32 min ago - IT industry leaders
13 hours 55 min ago - Reply to comment | Linux Journal
1 day 6 hours ago - Reply to comment | Linux Journal
1 day 9 hours ago - Reply to comment | Linux Journal
1 day 10 hours ago - great post
1 day 11 hours ago - Google Docs
1 day 11 hours ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.





Comments
need to develop a software in java
hey buddy
i need to make a software for a billing to a customer suggest me a tool
Makefiles please
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?
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
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
Both Glade 2 and Libglade are available on windows
http://gladewin32.sourceforge.net/
Re:Both Glade and Libglade are available on windows
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
This article claims that ExampleSwing.java and ExmapleAWT.java were compiled with gcj, for example:
"Listing 3 can be compiled with gcj --main=ExampleSwing -o ExampleSwing ExampleSwing.java."
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.
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?
There's a windows compilation howto here:
http://java-gnome.sourceforge.net/cgi-bin/bin/view/Main/WindowsCompilation
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
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 runExample.sh 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/TestGTK.java
gij testgtk.TestGTK
cd /usr/local/src/libglade-java-2.10.1/doc/examples
gcj -C glade/Test.java
gij glade.Test
maybe the runExample.sh script needs to be patched to properly handle MinGW's gcj/gij. maybe i'll do that... tomorrow.
Do it in C or Python
And don't expect Linux or Gnome users to use your java app.
Whats with the anti-Java attitude
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
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
That users (either you) wont even know it was coded in Java :-)))
Thats the most nice thing about gcj... creating native binaries.
a question
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 ExampleGNOME.glade with the compiled app?
Last time I was checking on G
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
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
Not if you use LibGlade, AFAIK. And LibGlade is recommended now, instead of C code generation...
Great article! If you comp
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-)
The "hello world" lines are cult and have to begin every programming-lesson 8-)))))))
This is a great article!
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?