2016-07-07 4 views
2

私は2段階でアプリケーションを持っていますが、私はユーザーに2段階目を終了させたくありません。システムトレイの近くでjavafxステージを最小化するにはどうすればよいですか?

現時点では私はウィンドウを最小化するためにoncloseRequestハンドラを使用しています -

secondaryStage.setOnCloseRequest(event -> { 
    secondaryStage.setIconified(true); 
    event.consume(); 
}); 

私は、ユーザーがウィンドウを閉じたときにシステムトレイにアイコンを表示したいと思います。そして、ユーザーはトレイからウィンドウを再び開くことができるはずです。

また、プライマリステージが閉じられていることを確認するにはどうすればいいですか?第2ステージも閉じますか?

+2

n javafxのビルドイン機能これは、システムトレイにアプリケーションをアイコン化するためのいくつかのツールを使用して(Windowsで)行うことができます。 httt://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.htmlそして、あなたがドイツ語を話すのなら、javafx:http:// blogにそれを起こすいいガイドがあります。 essential-bytes.de/wie-man-javafx-applikationen-in-das-system-tray-verbannt/またはjewelseaから英語で:https://gist.github.com/jewelsea/e231e8​​9e8d36ef4e5d8a – NwDev

+0

ありがとう!また、プライマリステージが閉じているときに2番目のウィンドウを閉じるにはどうしたらいいですか? –

答えて

0

また、プライマリステージが閉じていることを確認するには、第2ステージ も閉じますか?

プライマリステージが閉じているときに二段階を閉じるために、このようなものを使用することができます。

近くに設定された後、startメソッドで

Platform.setImplicitExit(false); 

を次のプロパティを設定し

primaryStage.setOnCloseRequest((WindowEvent we) -> { 
    secondaryStage.close(); 
} 
+0

他のonCloseRequestハンドラのために、それは単にsecondaryStageをアイコン化しませんか? – Puce

+0

ifステートメントをセカンダリのonCloseRequestハンドラに追加して、プライマリステージが閉じられているかどうかを調べることができます – Kaman

1

イベント

secondaryStage.setOnCloseRequest(event -> { 
    // Your code here 
}); 

ystemトレイは、次のコード試してみてください。

オリジナル文書へのリンク:https://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html

//Check the SystemTray is supported 
    if (!SystemTray.isSupported()) { 
     System.out.println("SystemTray is not supported"); 
     return; 
    } 
    final PopupMenu popup = new PopupMenu(); 

    URL url = System.class.getResource("/images/new.png"); 
    Image image = Toolkit.getDefaultToolkit().getImage(url); 

    final TrayIcon trayIcon = new TrayIcon(image); 

    final SystemTray tray = SystemTray.getSystemTray(); 

    // Create a pop-up menu components 
    MenuItem aboutItem = new MenuItem("About"); 
    CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size"); 
    CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip"); 
    Menu displayMenu = new Menu("Display"); 
    MenuItem errorItem = new MenuItem("Error"); 
    MenuItem warningItem = new MenuItem("Warning"); 
    MenuItem infoItem = new MenuItem("Info"); 
    MenuItem noneItem = new MenuItem("None"); 
    MenuItem exitItem = new MenuItem("Exit"); 

    //Add components to pop-up menu 
    popup.add(aboutItem); 
    popup.addSeparator(); 
    popup.add(cb1); 
    popup.add(cb2); 
    popup.addSeparator(); 
    popup.add(displayMenu); 
    displayMenu.add(errorItem); 
    displayMenu.add(warningItem); 
    displayMenu.add(infoItem); 
    displayMenu.add(noneItem); 
    popup.add(exitItem); 

    trayIcon.setPopupMenu(popup); 

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

例システムトレイの画像:

System tray program example

を使用すると、以下のことをfollwもAWTイベントハンドラからのJavaFXのメソッドを呼び出すにはway:

yourAwtObject.addActionListener(e -> { 
    Platform.runLater(() -> primaryStage.show()); 
}); 
関連する問題