If the system doesn't support systray SystemTray.getSystemTray() will failed

This commit is contained in:
Laurent Clouet
2015-09-07 20:10:06 +01:00
parent c5033faab2
commit 56b7054ca2

View File

@@ -52,8 +52,7 @@ public class GuiSwing extends JFrame implements Gui {
WORKING, SETTINGS WORKING, SETTINGS
} }
private static final SystemTray TRAY = SystemTray.getSystemTray(); private SystemTray sysTray;
private JPanel panel; private JPanel panel;
private Working activityWorking; private Working activityWorking;
private Settings activitySettings; private Settings activitySettings;
@@ -81,6 +80,13 @@ public class GuiSwing extends JFrame implements Gui {
e1.printStackTrace(); e1.printStackTrace();
} }
try {
sysTray = SystemTray.getSystemTray();
}
catch (UnsupportedOperationException e) {
sysTray = null;
}
URL iconUrl = getClass().getResource("/icon.png"); URL iconUrl = getClass().getResource("/icon.png");
if (iconUrl != null) { if (iconUrl != null) {
ImageIcon img = new ImageIcon(iconUrl); ImageIcon img = new ImageIcon(iconUrl);
@@ -214,14 +220,14 @@ public class GuiSwing extends JFrame implements Gui {
} }
public void hideToTray() { public void hideToTray() {
if (SystemTray.isSupported() == false) { if (sysTray == null || SystemTray.isSupported() == false) {
System.out.println("GuiSwing::hideToTray SystemTray not supported!"); System.out.println("GuiSwing::hideToTray SystemTray not supported!");
return; return;
} }
try { try {
trayIcon = getTrayIcon(); trayIcon = getTrayIcon();
TRAY.add(trayIcon); sysTray.add(trayIcon);
} }
catch (AWTException e) { catch (AWTException e) {
System.out.println("GuiSwing::hideToTray an error occured while trying to add system tray icon (exception: " + e + ")"); System.out.println("GuiSwing::hideToTray an error occured while trying to add system tray icon (exception: " + e + ")");
@@ -233,9 +239,11 @@ public class GuiSwing extends JFrame implements Gui {
} }
public void restoreFromTray() { public void restoreFromTray() {
TRAY.remove(trayIcon); if (sysTray != null && SystemTray.isSupported()) {
sysTray.remove(trayIcon);
setVisible(true); setVisible(true);
} }
}
public TrayIcon getTrayIcon() { public TrayIcon getTrayIcon() {
final PopupMenu trayMenu = new PopupMenu(); final PopupMenu trayMenu = new PopupMenu();