2013-01-31 9 views

risposta

27

La chiave qui è impostare l'uscita implicita su false Platform.setImplicitExit(false); Inoltre è importante mostrare e nascondere lo stage in una nuova discussione.

Platform.runLater(new Runnable() { 
    @Override 
    public void run() { 
     stage.show(); 
    } 
}); 

Platform.runLater(new Runnable() { 
    @Override 
    public void run() { 
     stage.hide(); 
    } 
}); 

Avanti, l'intero codice:

import java.awt.AWTException; 
import java.awt.MenuItem; 
import java.awt.PopupMenu; 
import java.awt.SystemTray; 
import java.awt.TrayIcon; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import java.net.URL; 
import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.event.EventHandler; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.stage.WindowEvent; 
import javax.imageio.ImageIO; 

/** 
* 
* @author alvaro 
*/ 
public class TrayTest extends Application { 

    private boolean firstTime; 
    private TrayIcon trayIcon; 


    public static void main(String[] args) 
    { 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     createTrayIcon(stage); 
     firstTime = true; 
     Platform.setImplicitExit(false); 
     Scene scene = new Scene(new Group(), 800, 600); 
     stage.setScene(scene); 
     stage.show(); 

    } 

    public void createTrayIcon(final Stage stage) { 
     if (SystemTray.isSupported()) { 
      // get the SystemTray instance 
      SystemTray tray = SystemTray.getSystemTray(); 
      // load an image 
      java.awt.Image image = null; 
      try { 
       URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg"); 
       image = ImageIO.read(url); 
      } catch (IOException ex) { 
       System.out.println(ex); 
      } 


      stage.setOnCloseRequest(new EventHandler<WindowEvent>() { 
       @Override 
       public void handle(WindowEvent t) { 
        hide(stage); 
       } 
      }); 
      // create a action listener to listen for default action executed on the tray icon 
      final ActionListener closeListener = new ActionListener() { 
       @Override 
       public void actionPerformed(java.awt.event.ActionEvent e) { 
        System.exit(0); 
       } 
      }; 

      ActionListener showListener = new ActionListener() { 
       @Override 
       public void actionPerformed(java.awt.event.ActionEvent e) { 
        Platform.runLater(new Runnable() { 
         @Override 
         public void run() { 
          stage.show(); 
         } 
        }); 
       } 
      }; 
      // create a popup menu 
      PopupMenu popup = new PopupMenu(); 

      MenuItem showItem = new MenuItem("Show"); 
      showItem.addActionListener(showListener); 
      popup.add(showItem); 

      MenuItem closeItem = new MenuItem("Close"); 
      closeItem.addActionListener(closeListener); 
      popup.add(closeItem); 
      /// ... add other items 
      // construct a TrayIcon 
      trayIcon = new TrayIcon(image, "Title", popup); 
      // set the TrayIcon properties 
      trayIcon.addActionListener(showListener); 
      // ... 
      // add the tray image 
      try { 
       tray.add(trayIcon); 
      } catch (AWTException e) { 
       System.err.println(e); 
      } 
      // ... 
     } 
    } 

    public void showProgramIsMinimizedMsg() { 
     if (firstTime) { 
      trayIcon.displayMessage("Some message.", 
        "Some other message.", 
        TrayIcon.MessageType.INFO); 
      firstTime = false; 
     } 
    } 

    private void hide(final Stage stage) { 
     Platform.runLater(new Runnable() { 
      @Override 
      public void run() { 
       if (SystemTray.isSupported()) { 
        stage.hide(); 
        showProgramIsMinimizedMsg(); 
       } else { 
        System.exit(0); 
       } 
      } 
     }); 
    } 
} 
+2

Questo era quello che stavo cercando ... ottima risposta –

+0

JavaFx 8 è già disponibile. È ora possibile utilizzare systray senza AWT? JavaFx sembra molto più bello. – qed

+0

Questo non funziona per me su OS X. Quando passa il mouse sopra l'icona del vassoio, si blocca. – Ascherer

0

Per quanto ne so, sarà possibile in JFX 8. Al momento la soluzione migliore è incorporare l'applicazione in AWT e nascondere la finestra AWT stessa.

+0

potete darmi l'esempio di lavoro di questo tipo che hai spiegato ... –

+1

@ la risposta di alscu era ciò che hai spiegato .. Grazie –

+0

JavaFx 8 è già disponibile. È ora possibile utilizzare systray senza AWT? JavaFx sembra molto più bello. – qed