Monday, May 23, 2011

Java Tray Icon Transparency Under Linux

Well, like many people before me, I had problems with my tray icons under linux. The JDK implementation has a bug that renders tray icons with transparency with an ugly gray background instead of the transparent background.

This is really annoying to me and I really don't like the idea of providing an application that looks half-assed. So, I actually got around this problem creating a small native library that implements the tray icon using gtk.

So, if you end up having tray icon problems, you may use my little artifact. The source code is here. I also deployed in the central, you can use it putting the following dependency:

<groupId>com.github.taksan</groupId>
<artifactId>native-tray-adapter</artifactId>
<version>1.1</version>

The usage is very simple:

 SystemTrayAdapter trayAdapter = SystemTrayProvider.getSystemTray();  
 URL imageUrl = getClass().getResource("myImage.svg");  
 String tooltip = "I'm transparent under linux!";   
 PopupMenu popup = produceMyPopupMenu();  
 TrayIconAdapter trayIconAdapter = trayAdapter.createAndAddTrayIcon(  
    imageUrl,   
    tooltip,  
    popup);  

Enjoy!

4 comments:

  1. Does not work as expected in ubuntu 11.04 (with unity). Tray icon does not show up, and info messages from it shows up in upper left corner. It also seem to block the application from exiting. Any workaraounds or fixes?

    ReplyDelete
  2. Hi Gunnar

    I wasn't aware of this effect under unity. I'll have to install it and check what is going wrong.

    Thanks

    ReplyDelete
  3. Hi Gunnar

    My proposed solution uses a native implementation with GtkStatusIcon, which is not "supported" by Unity anymore.

    I found out that it is possible to add the application to a whitelist so it will show up under unity. For example:

    http://www.webupd8.org/2011/04/how-to-re-enable-notification-area.html

    Besides adding "all", I suppose you could add "java". I'm trying to figure out a way to find whether unity is running, so the library will fallback to java implementation (but in this case, without transparecy).

    ReplyDelete
  4. Did you find a solution for recent Unity versions? I would like to use this in my project as well. Thanks, R

    ReplyDelete