To hide a JavaFx fxml or JavaFx swing application in the system tray

I want to develop a client application for a website.

I want the application to be in the system tray while minimizing.

I do not know how to complete this task.

This is their example for this type of operation.

+6
source share
2 answers

The key here is to set the implicit output to false Platform.setImplicitExit(false); It is also important to show and hide the scene in a new stream.

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

Next, all the 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); } } }); } } 
+27
source

As far as I know, this is possible in JFX 8. Currently, the best solution is to embed your application in AWT and hide the AWT window itself.

0
source

All Articles