Back to Blog home

Java GUI developers, suffer no more ;-)

Published on Monday August 13, 2007 by Andreas Aardal Hanssen in Qt Graphics View Qt Jambi | Comments

I wrote my first Jambi program today (That's Qt for Java, btw). I admit to being one hard core C++ developer, but having nothing against Java as a language... as opposed to its horrible APIs. Which is also why I've shyed away from Qt Jambi, making little sarcastic remarks to those of my colleagues that have done all the work implementing it. But I coincidentally found myself in a situation where I was forced to write a Jambi application. Or rather, port a C++ application to a Jambi .java file. And uhm, I guess you can say I was positively surprised. :-)

Screenshot of a Jambi application
My First Qt Jambi app, showing perspective transformations using Graphics View.

Since let's face it, beyond the difference in syntax there's nothing very different between these two languages (that I care about anyway). Now I am a C++ enthusiast, but I do still think garbage collection is nice sometimes [*], although _constantly_ having to "new" everything is somewhat annoying, just look at the source at the bottom of this post. Native C++ generates library files that need to be in LD_LIBRARY_PATH or somehow known to the runtime when you launch your app. And likewise, Java apps need to know their .jars and .class files. But language details is really not the issue when I choose a tool for a job.

class CustomScene extends QGraphicsScene {
protected void drawBackground(QPainter painter, QRectF exposed) {
painter.fillRect(sceneRect(), new QBrush(new QColor(230, 240, 255)));
painter.setPen(new QPen(QColor.lightGray, 1));

I downloaded the Jambi Linux32 GPL OpenSource package from ftp://ftp.trolltech.com/qtjambi/source, and unpacked it into my home directory as /home/ahanssen/qtjambi-linux32-gpl-4.3.1_01. With some help from Eskil, who pointed out that the package contains a .jar file that basically is all you need to worry about, I spent about 15 minutes porting my app to Jambi. Why so long? Well, because there are a few things the fresh Jambi developer needs to know about what the Qt API looks like in the Java world. And (chuckle) I was too lazy to look up the complete docs at doc.trolltech.com. They even made JavaDoc documentation. Awwww... ;-)

  • Where C++ uses namespacing with ::, Java uses a '.' instead.
  • For enums, you need to include the name of the enumerator like this: Qt.Orientation.Horizontal.
  • Java doesn't understand a->b ;-), hehe. Just search & replace your "->" with "." right away.
  • Typically all qualifiers for function args go away. So "const QRectF &" becomes "QRectF".
  • There's no stack-allocation, so setPen(QPen()) becomes setPen(new QPen()).
  • Qt Jambi uses Java's native types, so instead of qreal, etc, just use double.

I encourage you to try it out, it's really easy. And it _is_ Qt. To run my Jambi program, all I did was
type this into my console:

# Compile
ahanssen@schizo:~> javac -cp qtjambi-linux32-gpl-4.3.1_01/qtjambi.jar Perspective.java

# Run
ahanssen@schizo:~> java -cp qtjambi-linux32-gpl-4.3.1_01/qtjambi.jar:. Perspective

Note: I didn't modify my env variables at all to get this working.

Download the source file from http://www.andreas.hanssen.name/Perspective.java, follow the above instructions, and see if it works for you :-).

[*] You cannot believe how much crap I've had to deal with because of explicit deleting of objects in Graphics View ;-). It all works fine now, but in a GC world, it wouldn't have been an issue...

Subscribe to Our Blog

Stay up to date with the latest marketing, sales and service tips and news.

We are updating our comment system and you could face some issues. Please write to us at feedback@qt.io to report issues/bugs.