2013-01-31 9 views
6

私はWebサイト用のクライアントアプリケーションを開発したいと思います。システムトレイにJavaFx fxmlまたはJavaFx swingアプリケーションを表示しない

最小化すると、アプリがシステムトレイに存在するようにします。

このタスクの実行方法はわかりません。

このタイプの操作の例です。

+0

正解は[こちら](http://stackoverflow.com/a/12571924/1624376) – Aubin

答えて

27

ここで重要な点は、暗黙の終了をfalseに設定することです。Platform.setImplicitExit(false); 新しいスレッドでステージを表示および非表示にすることも重要です。

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

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

次に、全体のコード:

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

これは私が探していたものでした....すばらしい答え –

+0

JavaFx 8は既に出ています。 AWTなしでsystrayを使用できるようになりましたか? JavaFxははるかに良く見えます。 – qed

+0

これはOS Xで私にとってはうまくいきません。トレイアイコンの上にマウスを置くと、トレイアイコンがハングアップします。 – Ascherer

0

私が知る限り、JFX 8で可能です。今すぐベストな解決策は、アプリケーションをAWTに埋め込み、AWTウィンドウ自体を隠すことです。

+0

は、あなたが私にこの作業の例を与えることができますあなたが説明したタイプ... –

+1

@ alscuの答えはあなたが説明したものでした。ありがとう –

+0

JavaFx 8は既に出ています。 AWTなしでsystrayを使用できるようになりましたか? JavaFxははるかに良く見えます。 – qed

関連する問題