thefoggiest.dev

QT4 look and feel for Swing

UPDATE (2009-05-09): The ‘laf’ option to the Netbeans executable should be preceeded by a double dash (–laf) instead of a single one. Corrected.

From the first moment that I used Linux, some 9 years ago, I was a KDE user. When KDE 4 came, I switched to Gnome, because I found KDE 4 unstable and slow. I like eye-candy, but not at the expense of speed.

However, I’ve been trying out Kubuntu 9.04 for a few days now, and I must say I like it. It is faster than Gnome with Compiz, just as stable and it looks way better than Gnome. That, and the one complaint I have about Gnome, its lack of functionality to protect users for getting confused, is slowly converting me back to KDE again.

But since Java is my language of choice whenever databases are concerned, I write my desktop applications for personal use in Swing. I hate the default Metal look and feel, so I’m happy to report that I have found out how to give Swing applications a QT4 look.

First, you have to install a gtk qt4 engine. On Kubuntu, you do that like this:

# sudo apt-get install gtk-qt-engine-kde4

In your swing application, insert this piece of code into the main method:

try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception ex) {
    System.out.println (ex);
}

If you’re using the Netbeans Application Framework, insert it in the constructor of xView.java, where x is usually the name of your project.

In addition, if you want Netbeans to look QT4 as well, start it like this:

# /opt/netbeans-6.5.1/bin/netbeans --laf com.sun.java.swing.plaf.gtk.GTKLookAndFeel


Categorised as: howto, java, linux


4 Comments

  1. […] My Cup of Tea Stuff I find interesting. « QT4 look and feel for Swing […]

  2. Reza.M says:

    Hi,
    is there KDE4 LookAndFeel Available for Java UIManager?
    if no, how i can build the new LookAndFeel?

    Thanks.

  3. thefoggiest says:

    Hi Reza.M, the above gives my applications a KDE look-and-feel in KDE and a Gnome GTK look and feel in Gnome/XfCE et al. It should work for you too.

  4. […] the command line and using SAF it is in fact pretty easy to make Swing use the GTK widgets. This way Java applications get a native look and feel under […]