12

I want to develop a client app for website .

I want the app to reside in system tray when minimised.

I dont know how to accomplish this task .

Is their any example for this type of operation.

Mohammad Sadiq Shaikh
  • 3,160
  • 9
  • 35
  • 54

2 Answers2

37

The key here is to set the implicit exit to false Platform.setImplicitExit(false); Also is important to show and hide the stage in a new thread.

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

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

Next, the whole code:

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);
                }
            }
        });
    }
}
juliocb92
  • 96
  • 2
  • 12
alscu
  • 928
  • 9
  • 22
  • 1
    JavaFx 8 is out already. Is it now possible to use systray without AWT? JavaFx looks much nicer. – qed Aug 02 '14 at 22:01
  • This doesn't work for me on OS X. When i hover over the tray icon, it hangs. – Ascherer Sep 05 '15 at 20:48
  • @Ascherer Did you find a solution for OS X? – thanili Nov 11 '15 at 23:03
  • After I close the application ( hide ), the application Thread ( graphical one ) closes and it's not possible to restore from system tray after that. I'm using java 8u66. I think stage.hide() method is breaking the operation. – Guerino Rodella Nov 26 '15 at 14:41
  • I found a solution for my issue. There is a stackoverflow link which follows. http://stackoverflow.com/questions/32355335/on-javafx-how-to-hide-stage-without-disposing-it-and-closing-the-application – Guerino Rodella Nov 26 '15 at 16:46
  • Good answer. I suggest a modification in the showListener `if (!stage.isShowing()) stage.show(); else if (stage.isIconified()) stage.setIconified(false); else hide(stage);` – Solostaran14 Feb 26 '16 at 15:04
  • The key here is to set the implicit exit to false `Platform.setImplicitExit(false);` Also is important to show and hide the stage in a new thread. – juliocb92 Feb 01 '18 at 16:03
  • @thanil, @Ascherer To solve the OS X hangs problem remove the call to `trayIcon.displayMessage´ – Rafael Membrives Mar 20 '19 at 10:53
0

As far as I know it will be possible in JFX 8. Right now the best solution is to embed your application into AWT and hide the AWT window itself.

Lakatos Gyula
  • 3,949
  • 7
  • 35
  • 56