2013-01-31 5 views

답변

27

여기서 핵심은 암시 적 종료를 거짓으로 설정하는 것입니다. 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

관련 문제