29

I'm making a Java Application on Linux that uses sytray using Java 6 and Swing. The app looks great (uses the system look and feel) but the systray looks awful. I mean the systray menu looks like old widgedts (Motif?). I wonder if there is a way to set a look and feel or something to make the system tray prettier.

Heres a screenshot of the tray: enter image description here

Marcos Roriz Junior
  • 4,076
  • 11
  • 51
  • 76
  • 2
    I've deleted my answer, since it appears that `UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());` does not get applied to these menus. +1, good question. –  Jan 25 '11 at 02:57
  • 1
    @pst: indeed? That's exactly what I remember Motif to look like. – Joachim Sauer Jan 31 '11 at 10:04
  • After testing the `JPopupMenu` recommendations that most will eventually stumble upon (including a very nice `JXTrayIcon` class from sunlabs) the menu STILL hangs in Linux. After many attempts, I found out that binding a JPanel's visibility as the trigger to the popup will allow it to behave properly. Here is the only solution which works for me in Linux. Warning, it adds a slight delay but should fix the showing/hiding of a `JPopupMenu`. https://gist.github.com/tresf/d826eaeecfa887e576f0 – tresf Dec 20 '15 at 20:19

6 Answers6

8

Have you tried JXTrayIcon?

I tested this demo from SwingHelper on Ubuntu 10.10 with Compiz and it looks cool.

UPDATE

As 2020, these links are broken and this solution has many drawbacks today. For instance, GNOME3 desktop environments had removed entirely system tray icons and they replaced it with AppIndicator.

Java's (AWT/Swing) System Tray support is broken today. I recommend using this Java library: https://github.com/dorkbox/SystemTray

From the site:

Professional, cross-platform SystemTray support for Swing/AWT, GtkStatusIcon, and AppIndicator on Java 6+. This library provides OS Native menus and Swing/AWT menus, depending on the OS and Desktop Environment and if AutoDetect (the default) is enabled.

For reference, you can found a copy of the original example at here

gorlok
  • 1,155
  • 6
  • 16
2

Swing uses emulated UI widgets. It has a number of styles or themes you can apply. If you would prefer more native results you will need to look for another widget toolkit. You have a few options:

  1. If your needs are very basic, you may be happy with AWT that is the original Java UI toolkit. It uses native widgets, but has very limited library of widgets that it supports.

  2. If you want to go beyond AWT, consider SWT, which is maintained at eclipse.org. It gives you a rich library of widgets, that are implemented natively.

Konstantin Komissarchik
  • 28,879
  • 6
  • 61
  • 61
  • Sorry I think I miswrote the question, I mean the App uses and render beautifully is only the systray that sucks. – Marcos Roriz Junior Jan 25 '11 at 02:54
  • I would guess that you are missing look-n-feeling setting in your systray code... There was another answer with code snippet for how to set look-n-feel, but it appears to be gone now. – Konstantin Komissarchik Jan 25 '11 at 02:57
  • If you don't get a better answer, I would recommend trying AWT for the systray menu, while leaving the rest of the app as Swing. AWT is fully capable of rendering this menu, is already present in the JRE and you don't have to contend with look-n-feel biz. – Konstantin Komissarchik Jan 25 '11 at 03:00
2

Because Swing use AWT on Systray, if you want great looking on systray. Maybe you can try with SWT :)

martinusadyh
  • 1,120
  • 1
  • 8
  • 11
2

I wrote my own library for it. Here is the link:

http://www.2shared.com/file/sQdjb6aG/jtray.html

Usage:

import javax.swing.jtray.*;

JTrayIcon.initSystemTray();
JTrayIcon icon = new JTrayIcon(img, "Tooltip", jpopupmenu);
icon.displayMessage(null, "Title", "Multiline\nsecondline", 3000); // 3 seconds

The library uses a few dirty tricks, so maybe it may not work on any Linux platform as good as in Ubuntu. It should work for Windows and OSX as well.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1

I haven't tried it myself, but if you're using Java 6 Update 10 or later, can you use the new Nimbus look and feel?:

Using Nimbus LAF

I've heard of tray icons using "JPopupMenu" on Ubuntu, which uses the Nimbus look and feel, so this may be your best bet:

Using JPopupMenu in TrayIcon

From what I've seen, using JPopupMenu alone would be a big improvement - coupled with Nimbus it should be awesome.

Michael
  • 7,348
  • 10
  • 49
  • 86
1

A quick & dirty workaround: Create an undecorated JDialog, add a JPopupMenu for it and make it visible from your mouse listener of your TrayIcon as you want.

dejo
  • 11
  • 1