2017-08-16 27 views
0

由於某種原因,即使在關閉應用程序的窗口並退出之後,我的應用程序中仍存在一些進程。有沒有我沒有考慮過的東西?我的應用程序會定期生成系統托盤通知,但這些通知即使在退出後也會繼續。停止它的唯一方法是在任務管理器中找到java.exe進程。有我忽略的東西嗎?爲什麼JavaFX應用程序任務管理器進程在關閉時沒有結束

public class TrayNotification { 

    public static void execute(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException { 
     if (SystemTray.isSupported()) { 
      TrayNotification trayNotification = new TrayNotification(); 
      trayNotification.displayTray(firstName, incidentNumber, requestId, tabTable); 
     } 
    } 

    private void displayTray(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException { 
     // Obtain a single instance of SystemTray 
     SystemTray tray = SystemTray.getSystemTray(); 

     // Set TrayIcon values 
     Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/tray-icon.png")); 
     final TrayIcon trayIcon = new TrayIcon(image, "Remedy React Notification"); 
     trayIcon.setImageAutoSize(true); 
     trayIcon.setToolTip(incidentNumber); 

     // Add TrayIcon to SystemTray instance if possible 
     try { 
      tray.add(trayIcon); 
     } catch (AWTException e) { 
      // System.out.println("Notification could not be added."); 
     } 

     trayIcon.addActionListener(e -> { 
      Parent root = null; 
      try { 
       root = FXMLLoader.load(getClass().getResource("/fxml/scene.fxml")); 
       root.requestLayout(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
      System.out.println("Test"); 
     }); 

     // Display TrayIcon 
      trayIcon.displayMessage("Hey, " + firstName, "You are now tracking " + incidentNumber + " !", TrayIcon.MessageType.INFO); 

    } 

} 
+1

最可能的原因是創建非守護線程。你是在程序中創建任何新線程,無論是明確的還是通過執行者?考慮嘗試提供[MCVE]。 – Itai

+0

更多[here](https://stackoverflow.com/q/2213340/230513)和[here](http://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html #JLS-12.8)。 – trashgod

回答

0

我使用System.exit(0)當用戶點擊「退出」按鈕。

+0

我的問題是我沒有一種指定窗口「退出」按鈕的處理程序。我沒有一個退出菜單項,並不會執行一個......我想我需要沿着這篇文章的行:https://stackoverflow.com/questions/26619566/javafx-stage-close-handler – santafebound

+1

@santafebound - 如果您沒有「退出」選項,您如何定義「退出」?你說「這些在退出後仍然存在」 - 在「退出」時,調用'System.exit(0)'應該終止JVM實例。 – Itai

+0

您是否正在使用左上角/右上角的[X]來退出應用程序? –

相關問題