I ran into an Issue with Java awt tray icons under Ubuntu Gnome 16.04:
The Icon is displayed in the top left corner of my screen and in the System Tray appears a black square. The MouseListener is also not working (neither on the icon nor on the black square).
Here is my Code:
if (SystemTray.isSupported()) {
Image image = ImageIO.read(EyeUNIFYlocal.class.getResource("/star.png"));
TrayIcon trayIcon = new TrayIcon(image);
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked");
}
});
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException ex) {
System.err.println("Error while creating tray icon.");
}
} else {
System.err.println("Tray icons are not supported on this System.");
}
This code works fine on Windows 10.
Thank you in advance!
Java System Tray support doesn't exist for newer Linux distributions, mostly because of the changes from GtkStatusIcon to AppIndicator, and GTK2/3 changes as well (so, issues with JavaFX unless you install some additional libraries).
Additionally, since you mentioned Gnome -- Gnome likes to "hide" the AppIndicator as "notifications", so there is an extension (top-icons) that lets you restore the indicators back to the top of the screen (instead of in a hidden drawer at the bottom left of the screen)
If you want to display cross-platform system tray icons, I suggest the SystemTray project. There is an inbound 3.0 release soon (an API rewrite and better native support), but the older 2.x version should solve the problem you are having.
Related
I'm creating a Java program which pushes notifications and I want to remove or edit the line which says "Java(TM) Platform SE binary".
Is this possible? I searched on google but I couldn't see info about this.
Here is the related code. The last line is the line that pushes the notification.
public void mostrarNotificacion(Usuario user) throws AWTException, java.net.MalformedURLException, IOException {
//Obtain only one instance of the SystemTray object
SystemTray tray = SystemTray.getSystemTray();
//Get image
BufferedImage trayIconImage = ImageIO.read(getClass().getResource("favicon.png"));
int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH));
//Let the system resizes the image if needed
trayIcon.setImageAutoSize(true);
//Set tooltip text and notification text
if(user.getDescargos()>=5 || user.getOtrosPendientes()>=1){
mensaje = "Descargos pendientes: " + user.getDescargos() + "\nSecciones pendientes: "+ user.getOtrosPendientes();
}
trayIcon.setToolTip(mensaje);
tray.add(trayIcon);
trayIcon.displayMessage("Pendientes EKHI", mensaje, MessageType.WARNING);
}
"Java(TM) Platform SE binary" doesn't appear if you use TrayIcon.MessageType.NONE
Example Image:
The only way to solve this is to use a java native launcher.
My best choice would be javapackager which allows you to bundle a local copy of the JVM to make your app completely portable.
Other option is launch4j which just creates a launcher but still requires Java installed.
I would like to show some numbers on my tray icon indicating a number of events that happened to the user like what is done in this facebook notifications icons:
Do you think that it is possible ?
Thank you
You can do this using the TaskBar and TaskItem classes although it may not work on all platforms.
TaskBar taskBar = Display.getDefault().getSystemTaskBar();
// TODO may return null if not supported on the platform
// Get application item
TaskItem taskItem = taskBar.getItem(null);
if (taskItem != null)
taskItem.setOverlayText("your text");
Also try:
TaskItem taskItem = taskBar.getItem(shell);
where shell is your main application shell. The TaskItem JavaDoc suggests trying both methods of getting the TaskItem:
For better cross platform support, the application code should first
try to set this feature on the TaskItem for the main shell then on the
TaskItem for the application.
I'm trying to develop a Mac OsX app provided by a system tray icon, so after the first attempt with the simplest code to achieve it I noticed that every apps tray icon's (both system and user apps) on mac osX (10.8) allows to activate the relative popup menu with both left and right click on it but with my project only the left (MouseEvent.BOTTON1) button causes the popup menu to pulldown. Here's my code:
public class SystemTrayDemo
{
private SystemTray tray;
private TrayIcon tray_icon;
public SystemTrayDemo()
{
if (!SystemTray.isSupported())
{
JOptionPane.showMessageDialog(null, "System tray not supported!");
return;
}
else
tray = SystemTray.getSystemTray();
final PopupMenu popup = new PopupMenu();
MenuItem exit = new MenuItem("Exit");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (tray != null)
{
tray.remove(tray_icon);
System.exit(0);
}
}
});
popup.add(exit);
//add tray icon
tray_icon = new TrayIcon(getIcon("images/wifi.png"), "Open documents...", popup);
tray_icon.setImageAutoSize(true);
try
{
tray.add(tray_icon); // adds icon
}
catch (AWTException ex) {}
}
private Image getIcon(String name)
{
URL _url = getClass().getResource(name);
return new ImageIcon(_url).getImage();
}
public static void main(String args[])
{
new SystemTrayDemo();
}
}
but how I already said, only through left mouse button click.
So during a further attempt I've tried to mimic the behavior of the tray icons of every other apps using a MouseListener and firing a left button event on right click event using dispatchEvent() method like so:
public static void fireMouseEvent(Component c)
{
MouseEvent me = new MouseEvent(c, // which
MouseEvent.MOUSE_CLICKED, // what
System.currentTimeMillis(), // when
MouseEvent.BUTTON1_MASK, // no modifiers
0, 0, // where: at (10, 10}
1, // only 1 click
true); // popup trigger
c.dispatchEvent(me);
}
the event will handled by the mouse listener but obviously TrayIcon Class is not a Component subclass and therefore the source of MouseEvent is null and I get a NPE. Here's my MouseListener:
class MouseAdapt extends java.awt.event.MouseAdapter
{
public void mouseClicked(java.awt.event.MouseEvent me)
{
int button = me.getButton();
if(button == java.awt.event.MouseEvent.BUTTON3)
{
fireMouseEvent(me.getComponent());
}
}
}
try
{
tray.add(tray_icon); // aggiungi l'icona
tray_icon.addMouseListener(new MouseAdapt());
}
catch (AWTException ex) {}
Sorry for my english, I hope that someone who have ever had some experience with that kind of projects can help me. I've searched for hours but with no luck. Thank You for your help.
Edit: There's now a library working to fix all of this here: https://github.com/dorkbox/SystemTray
to activate the [TrayIcon] relative popup menu with both left and right click
This is simply not possible on Mac + Java currently. Using reflection to invoke the underlying triggers doesn't seem to help. This is a bug.
https://bugs.openjdk.java.net/browse/JDK-8041890
only the left (MouseEvent.BOTTON1) button causes the popup menu to pulldown. Here's my code
Even this is broken in some Java versions (7u79), fixed with an upgrade...
https://bugs.openjdk.java.net/browse/JDK-7158615
Cross-Platform TrayIcon Support:
Albeit slightly off-topic, I wanted to add, some projects use a JXTrayIcon to accomplish some fancy drop-down menus in Linux/Windows, etc. These also cause problems on Mac despite a click-bug it already suffers from today as well as bugs with Gnome3 requiring a completely separate hack. But on Mac, any attempt to use the decorated menus causes the menu to linger and is a very bad experience for the end-user. The solution I settled on was to use AWT for Mac, Swing for everything else. The Java TrayIcon support is in dire need of a rewrite. JavaFX claims to help this initiative, but it's staged for Java 9. In the mean time, I'm sticking to OS-dependent hacks.
Related Tray Issues for Other Platforms
Furthermore, some Linux distributions like Ubuntu have removed the tray icon by default in the Unity desktop, causing further headaches. https://askubuntu.com/a/457212/412004
In addition, the transparency of the icon is replaced with a gray color on Gtk/Gnome or Qt/KDE powered desktops (Both OpenJDK and Oracle JRE suffer this)
https://stackoverflow.com/a/3882028/3196753
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6453521
In addition, Gnome3 powered desktops may show it in the wrong corner, not at all, or it may show but be unclickable (Both OpenJDK and Oracle JRE suffer this)
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=660157
https://bugzilla.redhat.com/show_bug.cgi?id=1014448
In addition to that, high-DPI screens on Windows have a bug that draws the icon incorrectly: Windows 8 Distorts my TrayIcon
So in summary, the state of the System Tray in Java is OK, but due to the combination of factors is quite fragmented and buggy in JDK6, JDK7 and JDK8.
I'm writing an application that runs in the System Tray and notifies the user (i.e. makes one of those bubbles pop up) when something happens. The only problem is that the notifications only seem to work on Windows 7, and not Windows XP.
I've tested it on 2 Windows 7 computers (they've both worked) and 4 Windows XP computers (none of them have worked). No notification bubble is shown, and (as far as I know) no exceptions are thrown and everything else works as it should. I've even tested it on a Mac, and it worked, but it wasn't too pretty.
Here is a sample of my code.
private static TrayIcon trayIcon;
...
trayIcon = new TrayIcon(trayImage.getImage());
...
if (!SystemTray.isSupported())
{
System.out.println("SystemTray is not supported");
return;
}
final PopupMenu popup = new PopupMenu();
final SystemTray tray = SystemTray.getSystemTray();
trayIcon.setToolTip("Widget Name Here [" + role + "]");
...
try
{
tray.add(trayIcon);
}
catch (AWTException e)
{
System.err.println("TrayIcon could not be added.");
return;
}
...
//Here's where it doesn't work on XP
trayIcon.displayMessage("Connection error",
"Could not connect to server, please check your internet/VPN "
+ "connection", TrayIcon.MessageType.ERROR);
Any help would be much appreciated.
UPDATE: Ok, I've just confirmed that it's not a problem with my program, but a problem with the XP installations I've been testing them on. I ran the TrayIconDemo.java program found here on an XP computer and none of the notifications worked. I'm starting to think there's nothing I can do to get it to work on one of these computers...
Okay, so I've finally found the solution to my problem. It turned out that the following key was set to 0 (false) by default on the installations I was testing on:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\EnableBalloonTips
To enable the notification bubbles, simply set the value to 1.
I have Windows XP and working on system tray application. I have just added displayMessage() and it works just fine for me. Try to simplify your application. I am sure it will work. The find a bug.
EDIT: you didn't mentioned JRE version System try required Java6
this should be comment, but this is restrict for chars lenght
maybe, there is another reason in case of when Java updates are distributed by some of administrations tool for AD or ZenWorks, then sometimes (on both win7/Xp) is needed un-install all java instalactions and install fresh JRE manually (but stable 1.6.022 my view), sw distibutions ends without error, but all (???) classes/methods/changes cames from Java6 weren't accesible, on partial (RowSorter etc...) test ends with error, and I never search for knows Bugs on MS, Novell ...
How to/What is a good library, to create a fading indicator message in Java like that of Outlook when you get a message, or Ubuntu/Gnome when you've connected to a network?
Java 1.6 has a TrayIcon class that can be used to display notification messages.
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
tray.add(trayIcon);
trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
Here's the result:
On Linux you may also have a little program called notify-send. It makes it easy to invokes the standard freedesktop.org notification system from the shell. You can also run it from Java.
String[] notifyCmd = {"notify-send", "Hello, World!"};
Runtime.getRuntime().exec(notifyCmd);
I had to apt-get install libnotify-bin to get this on my Ubuntu box.
I've tested these things on Windows 7 and Ubuntu 9.10. In each case the notification disappeared after some time which is I suppose the fading indicator effect that you want.
I would use Trident animation library. Your task would be almost trivial if you use it.
Also you could take a look at Timing Framework, but it wasn't updated for a long time.