The system tray is not supported on the current platform?

久未见 提交于 2019-12-07 18:04:29

问题


I'm trying to make a System Tray application on Ubuntu 18.04 using Java.

This is the code that I'm executing:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class App {
    static{
        System.setProperty("java.awt.headless", "false");
    }
    public static void main(String[] args) {

//       if(!SystemTray.isSupported()){
//           System.out.println("System Tray is not supported.");
//           return;
//       }
       final PopupMenu popup = new PopupMenu();
       Image img = Toolkit.getDefaultToolkit().createImage("/path/img.png");
       final TrayIcon trayIcon = new TrayIcon(img);
       final SystemTray systemTray = SystemTray.getSystemTray();

       //create components of system tray
        trayIcon.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("In here!");
                trayIcon.displayMessage("Test","Some action happened",TrayIcon.MessageType.INFO);
            }
        });

        try{
            systemTray.add(trayIcon);
        }catch(AWTException e){
            System.out.println("TrayIcon could not be added.");
        }

    }


}

I commented out the isSupported() method test snippet because I kept getting "System tray is not supported".

The Exception I'm getting is:

Exception in thread "main" java.lang.UnsupportedOperationException: The system tray is not supported on the current platform. at java.awt.SystemTray.getSystemTray(SystemTray.java:186) at App.main(App.java:16)

Any idea how I would make it supported? Also, if anyone has a MacOS device, can you try it, and let me know if it works? Thanks!


回答1:


Gnome 3.28 (used in Ubuntu 18.04) removed the System tray. There is a software called TopIcon Plus Gnome Shell Exetension which returns the System tray. I tested the code, and the effects were as expected. An icon was placed in the Global bar.

https://extensions.gnome.org/extension/1031/topicons/



来源:https://stackoverflow.com/questions/50440268/the-system-tray-is-not-supported-on-the-current-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!