Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I minimize a javafx stage on close to the system tray?

I have an application with 2 stages, I dont want users to close the 2nd stage, just iconify it.

At present I am using the oncloseRequest handler to minimize the window -

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

I would like to show an icon in the system tray when a user closes the window. And the user should be able to reopen the window from the tray.

Also, how do I ensure when the primary stage is closed, the 2nd stage also closes?

like image 640
Darth Ninja Avatar asked Jul 07 '16 14:07

Darth Ninja


People also ask

What is the difference between stage and scene JavaFX?

stage. Stage , represents a window in a JavaFX desktop application. Inside a JavaFX Stage you can insert a JavaFX Scene which represents the content displayed inside a window - inside a Stage .

How do I close JavaFX?

exit(). The static exit() method in the Platform class is the preferred way to programmatically end a JavaFX program.

What is primary stage in JavaFX?

The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application. Stage objects must be constructed and modified on the JavaFX Application Thread.

What is window in JavaFX?

A top level window within which a scene is hosted, and with which the user interacts. A Window might be a Stage , PopupWindow , or other such top level. A Window is used also for browser plug-in based deployments.


1 Answers

Set the following property in start method

Platform.setImplicitExit(false);

Then set the on close event

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

To make a system tray try following code:

Original document link: 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.");
    }

Example system tray image:

System tray program example

To invoke method of Javafx from awt event handler you may follw the following way:

yourAwtObject.addActionListener(e -> {
    Platform.runLater(() -> primaryStage.show());
});
like image 111
Aupr Avatar answered Sep 20 '22 23:09

Aupr